Super Keyword in Java with Examples

Introduction

Super keyword in Java is simply a reference variable. It is used to refer the immediate parent class object.

Super comes in picture with Java Inheritance concept.

When we create an instance of a sub class then automatically an instance of parent class is created and that instance is referred by super reference in Java.

It is just the opposite of this keyword in Java as this refers to current class instance variable etc.

Let’s understand different uses of super keyword in Java :

Use of Super Keyword in Java

There are three main uses of super keyword in java, which are mentioned below :

  1. It can be used to refer instance variable of immediate parent class.
  2. It can be used to invoke method of immediate parent class.
  3. It can be used to invoke constructor of immediate parent class.
super keyword in java

Now , Let’s understand each usage of super keyword with Examples :

Super Keyword : To refer instance variable of immediate parent class

Java super keyword can be used to access the fields or data members of immediate parent class.

We can use super keyword to refer the immediate parent class’ field if the field name is same in both the classes (sub and parent).

Let’s look at below example :

Example 1: Super keyword in Java to refer immediate parent class instance variable

class Shape{  
    
String type="shape";

}  


class Circle extends Shape{ 
    
    
String type="circle";  

void printType(){  
    
System.out.println(type);//it will print type of Circle class  
System.out.println(super.type);// it will print the type of Shape class  

}  
}  
public class Test{  
    
public static void main(String args[]){  
    
Circle obj = new Circle();  
obj.printType();  

}}  

Output:

 circle 
 shape 

In the above example , both the classes Shape and Animal have the field type.

Now , if we refer type normally then the current class field in printed but when we are calling super.type then it will print the type property of parent class.

Super Keyword : To invoke method of immediate parent class

Super keyword can be used to invoke the methods of immediate parent class.

It is used when the immediate parent class and current class have same methods that means method overriding is in place.

To invoke the method of immediate parent class method , we use super keyword.

Let’s look at below example :

Example 2: Super keyword to invoke immediate parent class method

class Shape{  
    
public void draw(){
    System.out.println("Drawing Shape");
}

}  


public class Circle extends Shape{ 
    
    
public void draw(){
    super.draw();//it will call the parent class method
    System.out.println("Drawing Circle");
}

    
public static void main(String args[]){  
    
Circle obj = new Circle();  
obj.draw(); 


}} 

Output:

 Drawing Shape 
 Drawing Circle 

In the above example , both the classes Shape and Circle have the same method draw().

draw() method is overridden in the Circle class. To invoke the parent class method draw , we have use super.draw().

Super Keyword : To invoke constructor of immediate parent class

We can use super keyword to invoke the constructor of immediate parent class.

Let’s look at below example:

Example 2: Super keyword to invoke immediate parent class constructor

class Shape{  
    
 Shape(){
    System.out.println("Constructor of Shape class");
}

}  


public class Circle extends Shape{ 
    
    
Circle(){
    super();//it will call the parent class Constructor
    System.out.println("Constructor of Circle class");
    
}

    
public static void main(String args[]){  
    
Circle obj = new Circle();  


}}  

Output:

 Constructor of Shape class 
 Constructor of Circle class 

Additional Points to Remember

Compiler adds the super() in each class constructor automatically in case no super() and this() are present.

Let’s look at below example :

Example 1: super() is added automatically by Compiler

class Shape{  
    
 Shape(){
    System.out.println("Constructor of Shape class");
}

}  


public class Circle extends Shape{ 
    
    
Circle(){
    System.out.println("Constructor of Circle class");
    
}

    
public static void main(String args[]){  
    
Circle obj = new Circle();  


}}  

Output:

 Constructor of Shape class 
 Constructor of Circle class  

See Below Diagram :

super keyword in java

In the above figure, Compiler has automatically added the default constructor and super().

Frequently Asked Questions

Q. What is super() in java?

It is a keyword in Java. It is mainly used to refer the parent class instance variables, methods and constructor etc. It is used in Java Inheritance.

Q. What is difference between this and super keyword in java?

this keyword refers to the current class instance variable whereas super keyword refers to parent class instance variable.

Similarly, this invokes the current class method and constructor and super invokes the parent class constructor and method.

Q. Can we use super keyword in main method?

No, we cannot use super keyword inside the main method.

Q. Can we have this() and super() together?

We cannot use this() and super() together as both should be the first line of the constructor so either we can use this() or super().

Further Readings:

Conclusion

Super keyword in Java comes in picture with Java Inheritance. It is very important while working with parent classes and it has mainly below three usage :

  • refers parent class instance variable.
  • invokes parent class method.
  • invokes parent class constructor.

We have learnt all the above usage with examples and complete understanding of Java super keyword.

Thanks for Reading!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply