Inheritance of class - 03

We can inherit in Class…

  • By prototype
  • By super keyword
  • By setter method


By prototype:

class Student { constructor(name, age, title) { this.name = name; this.age = age; this.title = title; } get dateOfBirth() { return `His name is ${this.name}, and his age is ${this.age}`; } } Student.prototype.methodVal = function () { return `His name is ${this.name}, & his age is ${this.age}, he is a ${this.title}`; }; const parentClass = new Student("Mozahedul", 65, "student"); console.log(parentClass); console.log(parentClass.methodVal());

By Super keyword:

class Student { constructor(name, age, title) { this.name = name; this.age = age; this.title = title; } methodVal() { return `His name is ${this.name}, and his age is ${this.age}`; } get getterMethod() { return `Her name is ${this.name}, age is ${this.age}, & she is a ${this.title}`; } } class Teacher extends Student { constructor(name, age, title, subject) { super(name, age, title); this.subject = subject; } } // Parent class instance const parentClass = new Student("Mozahedul", 65, "student"); console.log("PARENT CLASS ==> ", parentClass.methodVal()); // Child class instances const childClass = new Teacher("mofizul", 15, "teacher", "mathematics"); console.log("CHILD CLASS ==> ", childClass); console.log("CHILD CLASS METHOD ==> ", childClass.methodVal()); console.log("PARENT CLASS GETTER METHOD ==> ", childClass.getterMethod);


Post a Comment

Previous Post Next Post