Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Inheritance lets you create a specialized class based on an existing one. The new class gets all properties and methods from the parent, plus its own unique features!
// Parent class
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound`);
}
}
// Child class inherits from Animal
class Dog extends Animal {
bark() {
console.log(`${this.name} barks!`);
}
}
const dog = new Dog('Buddy');
dog.speak(); // 'Buddy makes a sound' (inherited)
dog.bark(); // 'Buddy barks!' (own method)Child class inherits from parent
Use super() to call the parent's constructor
class Animal {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
class Dog extends Animal {
constructor(name, age, breed) {
super(name, age); // Call parent constructor
this.breed = breed; // Add child property
}
}
const dog = new Dog('Buddy', 3, 'Golden Retriever');
console.log(dog.name); // 'Buddy' (from parent)
console.log(dog.age); // 3 (from parent)
console.log(dog.breed); // 'Golden Retriever' (own)super() before using this!Initializing parent and child properties
Child class can define its own version of a parent method
class Animal {
speak() {
console.log('Animal makes a sound');
}
}
class Dog extends Animal {
speak() {
console.log('Dog barks!'); // Override parent method
}
}
class Cat extends Animal {
speak() {
console.log('Cat meows!'); // Override parent method
}
}
const animal = new Animal();
const dog = new Dog();
const cat = new Cat();
animal.speak(); // 'Animal makes a sound'
dog.speak(); // 'Dog barks!' (overridden)
cat.speak(); // 'Cat meows!' (overridden)Customizing parent methods
Call parent method and add child behavior
class Animal {
speak() {
console.log(`${this.name} makes a sound`);
}
}
class Dog extends Animal {
speak() {
super.speak(); // Call parent method first
console.log('Woof woof!'); // Then add child behavior
}
}
const dog = new Dog();
dog.name = 'Buddy';
dog.speak();
// 'Buddy makes a sound' (parent)
// 'Woof woof!' (child)Extending parent methods
class User {
constructor(username, email) {
this.username = username;
this.email = email;
}
login() {
console.log(`${this.username} logged in`);
}
}
class AdminUser extends User {
constructor(username, email) {
super(username, email);
this.isAdmin = true;
}
deleteUser(user) {
console.log(`Admin deleted ${user}`);
}
}
class GuestUser extends User {
login() {
console.log('Guest has limited access');
}
}
const admin = new AdminUser('admin', 'admin@site.com');
admin.login(); // 'admin logged in'
admin.deleteUser('baduser'); // 'Admin deleted baduser'
const guest = new GuestUser('guest', 'guest@site.com');
guest.login(); // 'Guest has limited access'E-commerce product hierarchy
class Parent {
constructor(param1) {
this.property1 = param1;
}
method1() {
// parent implementation
}
}
class Child extends Parent {
constructor(param1, param2) {
super(param1); // Call parent constructor
this.property2 = param2;
}
method1() {
super.method1(); // Call parent method (optional)
// child implementation
}
method2() {
// child-only method
}
}