Classical Inheritence

 1) Classical Inheritance:


Inheritance enables you to define a class that takes all the functionality from a parent class and allows you to add more. Using class inheritance, a class can inherit all the methods and properties of another class. Inheritance is a useful feature that allows code reusability.

To use class inheritance, we use the extends keyword.


Exercise:

1. Create a class "Person" with a property "name".

2. Create a method "greet()" in the class that prints "Hello {name}". 

3. Create a child class "Student" that inherits all methods and properties of the "Person" class.

4. Create an object of "Student" class and call the "greet()" method of "Person" class.












2)JavaScript super() keyword:

The super keyword used inside a child class denotes its parent class. 


Super keyword in JavaScript can be used to access and call on an object’s parent, it can be used in two ways.


As a function

As an object

Syntax:


super(arguments);

super.parentMethod(arguments);

Arguments: This keyword can accepts all the arguments that has been used to create a constructor.


Exercise:

1. Create a constructor of "Student" class that calls the constructor of "Person" class using super.


Neither the base (parent) nor derived (child) class requires a constructor, nor does the child class require a super() call. If you omit either constructor, an assumed one is present. 

However, if you do declare a constructor in a derived class, you'll need to call super() in it.


If you do not call super(), you only get into trouble if you access "this" in some manner.


Therefore, there are two ways to avoid typing super-constructor calls.


First, you can avoid accessing this by explicitly returning an object from the derived class constructor. However, this is not what you want, because the object created via new B() does not inherit A’s methods.


// Base class

class A {

    constructor() {}

}

// Derived class

class B extends A {

    constructor() {

        // No super-constructor call here!

        

        return {}; // must be an object

    }

}


Second, you can let JavaScript create default constructors for you:


// Base class

class A {

}

// Derived class

class B extends A {

}

This code is equivalent to:


// Base class

class A {

    constructor() {}

}

// Derived class

class B extends A {

    constructor(...args) {

        super(...args);

    }

}


When super() is used as a function:










When super() is used as an object of the parent class:














3)Overriding Method or Property:

If a child class has the same method or property name as that of the parent class, it will use the method and property of the child class. This concept is called method overriding.


Exercise:

1. Add a new property "occupation" in the "Person" class and assign the value "unemployed" to it. 

2. Override "occupation" in the "Student" class with value "student".

3. Override the "greet()" method of "Person" class in "Student" class to print:

    Hello student {name}.

    occupation: {occupation}




1) Here Hello student swati & occupation student is coming from line no. 19 and 20.
 The greet() method of derived class has overridden the parent's greet() method. 

2) And Hello Again swati is coming from line no. 10, from the greetAgain() method in parent class.

EXAMPLE 1: When a new method is added in the derived class



1) Here Hello student swati & occupation student is coming from line no. 19 and 20 (Derived class).

2) Hello Again swati is coming from line no.10 (Parent class).

3) Good Morning swati is coming from line no.23 (Derived class) from the new greetInTheMorning() method.

EXAMPLE 2: When super() is used as an object of parent class to invoke/call one of it's method/function




1) Here in line no. 22 super.greet() is used  as an object to call the Parent's class greet() method in line no.9 . So in the output, Hello swati is coming from line no.10 from the parent's greet() method.

2) Hello student swati & occupation swati is coming from line no.23 and 24, from the greet() method of the derived class in line no. 21.

4) Advantages of Inheritance:

1. Since a child class can inherit all the functionalities of the parent class, this allows code reusability.

2. Once a functionality is developed, you can simply inherit it. No need to reinvent the wheel. This allows cleaner and easily maintainable code.

3. Since you can also add your own functionalities in the child class, you can inherit only the useful functionalities and define other required features.








Comments