Super keyword

The super is a keyword in java which is used to refer parent class members and constructors.

Why do we need super keyword?


Whenever subclass and super class have same members then JVM gets ambiguity between such members(super class or subclass), so in order to resolve such ambiguity we can use super keyword in the subclass to refer super class members.

Super can be used in the below 3 scenarios


1) Super is used to invoke super class constructors from the subclass

2) Super is used to refer super class instance variables from the subclass

3) Super is used to invoke super class instance methods from the subclass

1) Super is used to invoke super class constructors from the subclass


The super keyword can be used to invoke the super class constructors.

Constructor are always called from bottom(subclass) to top (super class) and hence super class constructors are executed before the subclass constructors.

To invoke super class constructor from the sub class constructor, Java has provided “super” keyword.

This is an implicit keyword added automatically by compiler to invoke super class default constructor.

1
super(); //added implicitly inside the subclass constructor
super(); //added implicitly inside the subclass constructor

super(); is added by compiler only if there is no explicit call to the super class or same class constructor.


Example:


Employee.java

1
2
3
4
5
6
7
class Employee{
    
     Employee (){
    System.out.println("Employee class constructor”);
 
  }
}
class Employee{
    
     Employee (){
    System.out.println("Employee class constructor”);
 
  }
}


Developer.java

1
2
3
4
5
6
class Developer extends Employee {
Developer (){
   super(); //invoke parent class constructor
   System.out.println("Developer class constructor”); 
 }
}
class Developer extends Employee {
Developer (){
   super(); //invoke parent class constructor
   System.out.println("Developer class constructor”); 
 }
}


SuperConstructorTest.java

1
2
3
4
5
6
class SuperConstructorTest{
public static void main(String[] args) {
    Developer dev = new Developer();
 
  }
}
class SuperConstructorTest{
public static void main(String[] args) {
    Developer dev = new Developer();
 
  }
}



In the above program, we can see that super keyword is used to call super class constructor.

Rules for constructors with super


We can call any constructor either default or parameterized constructor of the super class using super keyword.

We use super(); to invoke default(no argument) constructor of super class

We use super(….) to invoke parameterized constructor of super class

If we use super, then super must be the first statement of every constructor

If a constructor does not explicitly invoke a super class constructor, then Java compiler automatically adds a call to the default (no-argument) constructor of the super class.

If the super class does not have a default (no-argument) constructor and if subclass constructor is not making call to appropriate constructor explicitly then we will get a compile-time error.

Since Object class always has such a default (no argument) constructor, so if Object class is the super class, then there is no problem.

If the subclass constructor invokes a constructor of its super class (explicitly/implicitly), then whole chain of constructors is called

from the specified class to the constructor of Object class. This is called as constructor chaining.

Refer constructor chain article for more details.

2) Super is used to refer super class instance variables from the subclass


Whenever both super class and subclass have same variable names, super keyword can be used to resolve the ambiguity between super class and subclass variables.

Example:


Employee.java

1
2
3
class Employee{
float bonus=10000;
}
class Employee{
float bonus=10000;
}


Developer.java

1
2
3
4
5
6
7
8
9
10
11
class Developer extends Employee {
float bonus=15000;
 
    void  display (){
 
    System.out.println("developer  bonus : ”+bonus);
 
    System.out.println("employee  bonus :+super.bonus);
 
  }
}
class Developer extends Employee {
float bonus=15000;
 
    void  display (){

    System.out.println("developer  bonus : ”+bonus);

    System.out.println("employee  bonus : ”+super.bonus);
 
  }
}


SuperConstructorTest.java

1
2
3
4
5
6
7
class SuperConstructorTest{
public static void main(String[] args) {
    Developer dev = new Developer();
    dev.display();
 
  }
}
class SuperConstructorTest{
public static void main(String[] args) {
    Developer dev = new Developer();
    dev.display();
 
  }
}



In the above example, both super class and subclass have a same variable called “bonus”.

We could access bonus variable of super class in subclass using super keyword.

3) Super is used to invoke super class instance methods from the subclass


Whenever super class and subclass have same methods(having method overridden) then to resolve the ambiguity we use super keyword in subclass to invoke the super class method.

Example:

1
2
3
4
5
6
7
class Employee{
float bonus=10000;
void greeting(){
System.out.println("Hello Employee”);
}
    
}
class Employee{
float bonus=10000;
void greeting(){
System.out.println("Hello Employee”);
}
    
}


Developer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Developer extends Employee {
float bonus=15000;
 
void greeting(){
System.out.println("Hello Developer”);
}
 
void  display (){
 
greeting(); //invoke current class greeting() method
 
super.greeting(); // invoke super class greeting() method
 
   }
}
class Developer extends Employee {
float bonus=15000;
 
void greeting(){
System.out.println("Hello Developer”);
}
 
void  display (){

greeting(); //invoke current class greeting() method

super.greeting(); // invoke super class greeting() method

   }
}


SuperConstructorTest.java

1
2
3
4
5
6
7
class SuperConstructorTest{
public static void main(String[] args) {
    Developer dev = new Developer();
    dev.display();
 
  }
}
class SuperConstructorTest{
public static void main(String[] args) {
    Developer dev = new Developer();
    dev.display();
 
  }
}




In the above example, we can see that, we have same method “greeting()” defined in both super class and subclass.

When we invoke this method without using super, it calls method in the same class and we used “super” keyword to invoke the method in the super class.

If we have different methods in super class and subclass then super is not required.

We can directly call the method in a normal way as there will be no ambiguity.

Note:

Its best practice to avoid using same variable names in both super class and subclass even though super can be used to resolve ambiguity.

Methods overriding is inevitable in many scenarios, so consider using super to invoke super class methods in that case.

Constructors of super class are automatically called by placing “super();” statement in each and every constructor of subclass, So we should use super only when we want to call any constructor of super class other than default no argument constructor.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on