Inheritance in JavaScript using classes. You have a Person class as the superclass, an Employee class that extends Person, and two subclasses (Designer and Developer)



Person Class:
- it has a constructor that takes a
nameparameter and initializes thenameproperty. - Defines a
playmethod.
Employee Class:
- Extends the
Personclass. - it has a constructor that takes a
nameand anidparameter, and initializes theidproperty.
Designer Class:
- Extends the
Employeeclass. - it has a constructor that takes a
nameand anidparameter. - Defines a
designmethod.
Developer Class:
- Extends the
Employeeclass. - it has a constructor that takes a
nameand anidparameter. - Defines a
codingmethod.
Creating instances and invoking methods:
- Creates instances of
DeveloperandDesigner. - Calls the
play,coding, anddesignmethods.
class Person //super class
{
constructor(name)
{
this.name=name
this.play=()=>{
console.log("playing");
}
}
}
//sub class
class Employee extends Person{
constructor(name,id)
{
super(name)
this.id=id
}
}
class Designer extends Employee{
constructor(name,id)
{
super(name,id)
this.design=()=>{
console.log("Designing.......")
}
}
}
class Developer extends Employee{
constructor(name,id)
{
super(name,id)
this.coding=()=>{
console.log("Coding.......")
}
}
}
const developer = new Developer("java codinggg",1)
const designer = new Designer("UX Design",1)
developer.play()
developer.coding()
designer.design()