Using the super keyword for constructors

Let's use the super keyword for constructors in this section. We'll use the previous example here too.

In the parentDemo class, we define a constructor, parentDemo(), and add a print statement to print: Parent class constructor.

In childDemo, we define a constructor childDemo() and add a print statement to print: Child class constructor. If we want to use the parentDemo class constructor in the childDemo class, we add the super() method in the childDemo() constructor. This makes the controller invoke the constructor in the parentDemo class.

There's an important rule we need to follow when working with constructors: whenever you use a super constructor in a child constructor, it should always be the first line in it.

When we run the childDemo class, the controller first executes the super() method. It goes to the parentDemo() constructor and executes it and then childDemo(). So the final output will be:

Parent class constructor
Child class constructor
QAClickAcademy
Rahul
I am parent class
I am in child class
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.178