javascript

Javascript: Class Inheritance

Inheritance is an important concept in Object Oriented Programming.

Inheritance is the process whereby one class, also called the sub-class, acquires the properties and methods of another class, also known as the super class.

To create a class inheritance in Javascript, we use the “extends” keyword.

class Animal{
 constructor(name){
        this.name = name;
    }
 sound(){
        console.log(`${this.name} makes sound`)
    }
}

class Cat extends Animal{
    myCat(){
        super.sound()
    }
}

let cat = new Cat("Fuzzy");
console.log(cat.myCat()); //Fuzzy makes sound

Notice that we use the “super” keyword to access the method from the parent class.

The class “Cat” now acquires the method of the super class “Animal”

Recent Posts

How To Migrate from PostgreSQL to MySQL

Database migration is one of the most challenging tasks in software engineering. While both PostgreSQL…

4 days ago

Hidden Gems Inside Modern JavaScript

Modern JavaScript isn’t just let, const, arrow functions, and promises anymore. Over the years, the language has…

4 days ago

Software Developer Pain Points Ranked: What Frustrates Developers the Most?

Software development is one of the most rewarding careers in technology, but it is also…

5 days ago

How to Print Documents in JavaScript

Printing a document in JavaScript usually means triggering the browser’s print dialog and controlling what…

7 days ago

CSS Display Cheatsheet

The display property controls how an element behaves in the layout and how its children are arranged. Access software…

2 weeks ago

10 JavaScript Habits Destroying Your Code

JavaScript is one of the most flexible programming languages ever created. That flexibility is powerful,…

2 weeks ago