Java super Keyword

Facebooktwitterredditpinterestlinkedinmailby feather

Java super Keyword

In Java, the keyword ‘super’ and ‘this’ are very similar.

The super keyword is a reference variable that refers to the parent class object. When we talk about super keyword, the Java inheritance comes in our mind, so to understand super keyword in java, we should have understood the concept of Java inheritance.

It is used to access the superclass methods and constructor.

It can pass the value from derived class constructor to base class constructor or data members.

The super() should be the first statement of derived class constructor.

Super cannot call the private methods or objects of the parent class; it can only call the public and protected.

It can’t be used in static methods.

Uses of Java Super keyword

Super can be used in the following context.

  • Use of super with variables
  • Use of super with methods
  • Use of super with constructors

Variable hiding by super keyword

If instance variable name is same in both superclass and sub-class, then sub-class instance variable hides the superclass variable.

Let’s have a look at the following example to understand the technique of variable hiding.

Example

Output

There are two instance variables, est, compName in both superclass and sub-classes. When we access the variables from displayCompdetail() method, then it prints the current class variable because the current class variable hides the superclass instance variable, which is having the same name.

Now, see the same example with the use of the super keyword.

Output

Now we can see in the given an example, we have used the super keyword then it calls the superclass variables and hides the sub-class variable.

Use of super with variables

When a child class and a parent class have same data member then we use the super keyword with variables to identify them. Super keyword also reduces the ambiguity because of variable. Let’s have a look at the following example.

Output-

In this example, when we have used the super keyword then it refers super class object and when we haven’t used super then it refers the current class object.

Example 2

Output

In the given example, when we use the super keyword then it refers the parent class object.

Use of super with methods

When a parent and child class have the same method, then we use the super keyword with methods to identify them. This process is called method overriding.

In the case of method overriding, we can use the keyword super along with the method to implement the method of the parent class.

Let’s have a look at the below example

Example 1

Output

Example 2

Another example will help you to understand the usage of super keyword with method.

Output-

Use of super with constructor

In the case of a constructor, super is also used to access the parent class constructor. Super can call parametric as well as a non-parametric constructor. Let’s have a look at the following example-

Example 1

Let’s understand how super keyword acts with parent class constructor.

Output

This is an example of use of super keyword with constructor.

Example 2

Output-

Facebooktwitterredditpinterestlinkedinmailby feather