Now let’s understand through examples How to call the constructor of a parent class.
Example: In this example, we are going to take two classes one is for the parent class and another is for the child class for inheriting the properties and methods of the parent class so for that we should have to use extends keyword to inherit the child class from the parent class. Then inside the child class constructor, we have to call the parent class constructor otherwise the compiler will throw an error and tell you to mention or call the constructor within the child class constructor method before accessing this.
Parent class method call child class method call
Example 2: This cannot be used in a child class constructor until super has been called. In ES6, constructors for subclasses are required to call super, or they must return some object in place of the one that was never initialized. In this example, Geeks class is the parent class of MyClass and when an object is created of type MyClass first it calls the MyClass constructor inside MyClass constructor we have to call the parent class constructor before accessing “this” (own object). So it first called the parent class(Geeks class)constructor before it access the object of its own.
inside Parent Class constructor inside Child class Constructor
Please Login to comment...