super keyword in java

In the previous session, we have seen the usage of method overriding. In this session, we will see how to use the keyword super in java programming.

The super keyword is used to refer immediate parent class members.

Objective of super keyword:

There are following objectives of using super keyword.

  1. Accessing super class or say parent class instance variables
  2. Calling methods in super class or say parent class
  3. Calling constructor in super class or say parent class

Let’s discuss them in details with examples.

1. Accessing super class or say parent class instance variables

The super keyword can be used to access variables present in its immediate parent class.

First understand the problem if super keyword is not used.

Consider, we have 2 classes where MobileStore is the parent class and myMobile is the child class. There is a common variable called mobile  in both the classes. We want to print parent’s class variable. But in code it prints variable from child class , even if child class is extending the parent class.

Output : Mobile is iPhone

Now, let’s see how this issue will be resolved by using the super keyword. We have refer the superclass variable bu using super keyword.

Output : Mobile is Samsung

 


2. Calling methods in super class or say parent class:

We can call the method of parent class by using super  keyword as shown in example.

Output : IN child class : 15000.00
In parent class : 10000.00

 

3. Calling constructor in super class or say parent class:

The super keyword can also be used to invoke the parent class constructor as shown below.

Note: If super keyword is not used to call constructor, compiler adds super keyword automatically while calling constructor.

We have commented the super keyword in the below code, even though we get the same output as above.

Output:
I am in parent class.
I am in child class

When is it possible to invoke the superclass method without using super keyword?

It is possible when, child class does not have the same method as present in parent class. Then, we can call the method directly in child class without using the super keyword.


What if we don’t extend the parent class and even though we use super() keyword?

This will not throw any error but a default constructor of Object class will be called. Every class extends Object class by default which is a parent class for all the classes.

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Print Friendly, PDF & Email
Method Overriding in Java
Initialization block in java
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *