// Class Inheritance in JavaScript
class Mammal {
constructor(name) {
this.name = name;
}
eats() {
return `${this.name} eats Food`;
}
}
class Dog extends Mammal {
constructor(name, owner) {
super(name);
this.owner = owner;
}
eats() {
return `${this.name} eats Chicken`;
}
}
let myDog = new Dog("Spot", "John");
console.log(myDog.eats()); // Spot eats chicken
//class in es6 are just functional constructor.
//Parent class is Person and Developer class inheritant from Person class using
//extend and super method
class Person{
constructor(firstname,lastname){
this.firstname= firstname;
this.lastname=lastname;
}
aboutPerson(){
console.log(`My name is ${this.firstname} ${this.lastname} `)
}
}
class Developer extends Person{
constructor(firstname,lastname,experience,projects){
/* //super keyword is used to call the constructor
of its parent class to access the parent's properties and methods*/
super(firstname,lastname);
this.experience=experience;
this.projects=projects;
aboutDev(){
console.log(`My name is ${this.firstname} and I have ${this.experience}
in software development`)
}
const ShirshakDev= new Developer('Shirshak','Kandel',3,13)
console.log(ShirshakDev.aboutDev())
// parent class
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello ${this.name}`);
}
}
// inheriting parent class
class Student extends Person {
}
let student1 = new Student('Jack');
student1.greet();