Use of SUPER keyword


 

Step1. Create a program and define the constructor in both the super and sub class. Constructor is always executed form the super class to the Sub class in OO concept. So if the object of the Sub class is created, then constructor of the sub class is called first and within this sub class constructor method using the SUPER keyword the super class constructor should be called other wise  we ll receive a compile time error. So mostly SUPER keyword is used in Inheritance concept where we have same method in both the SUPER & SUB class. Execute the program.

Step2. The O/P is as follows:

Step3. Let’s define a method in the super class and redefine it in the subclass as well. Call the Subclass method. Here calling the super class redefined method is optional but in case of constructor it is mandatory to call the super class constructor in the subclass constructor.

Step4. The O/P is as follows:


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s