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

Linux Steam Locomotive Bash program

What is Steam Locomotive (sl)? Steam Locomotive (sl) is a small terminal program on Unix/Linux systems…

3 weeks ago

Rate Limiting in Node JS

What is Rate Limiting? Download this article as a PDF on the Codeflare Mobile App…

4 weeks ago

JavaScript promise chaining

Learn on the Go. Download the Codeflare Mobile from iOS App Store.  1. What is…

1 month ago

UI/UX Design — Explained Like You’re 5

Download the Codeflare iOS app and learn on the Go 1. What UI and UX…

2 months ago

Costly Linux Mistakes Beginners Make

1. Running Everything as Root One of the biggest beginner errors. Many new users log…

2 months ago

How Keyloggers Work

A keylogger is a type of surveillance software or hardware that records every keystroke made…

3 months ago