Super is a keyword in Java that refers to the immediate superclass object.
Use of super keyword in Java:
- super can be used to call the immediate super class constructor (constructor chaining).
- super can be used to call an immediate superclass method on a subclass object from a subclass method.
- super can be used to access immediate superclass instance variables.
1. super can be used to call the immediate super class constructor (constructor chaining).
Example:
class Display { Display(){ System.out.println("Super class constructor called."); } } public class SuperExample1 extends Display { SuperExample1(){ //super keyword will call super class constructor. super(); System.out.println("Current class constructor called."); } public static void main(String args[]){ SuperExample1 obj = new SuperExample1(); } }
Output
Super class constructor called. Current class constructor called.
If super is not used explicitly compiler will automatically add super as the first statement.
class Display { Display(){ System.out.println("Super class constructor called."); } } public class SuperExample2 extends Display { SuperExample2(){ //compiler automatically add super here. System.out.println("Current class constructor called."); } public static void main(String args[]){ SuperExample2 obj = new SuperExample2(); } }
Output
Super class constructor called. Current class constructor called.
2. super can be used to call an immediate superclass method on a subclass object from a subclass method.
If superclass and subclass have the same methods and that method is called from the subclass method then the subclass method is called.
class Display { public void display(){ System.out.println("display method of super class."); } } class Show extends Display { public void display(){ System.out.println("display method of sub class."); } public void show(){ System.out.println("show method of sub class."); //subclass display method is called. display(); } } public class SuperExample4 { public static void main(String args[]){ //create Show class object. Show obj = new Show(); //method call obj.show(); } }
Output
show method of sub class. display method of sub class.
The above problem can be solved with the super keyword.
class Display { public void display(){ System.out.println("display method of super class."); } } class Show extends Display { public void display(){ System.out.println("display method of sub class."); } public void show(){ System.out.println("show method of sub class."); //super class display method is called. super.display(); } } public class SuperExample3 { public static void main(String args[]){ //create Show class object. Show obj = new Show(); //method call obj.show(); } }
Output
show method of sub class. display method of super class.
If the superclass and subclass do not have the same methods and the method of the superclass is called from the subclass method then the superclass method is called. There is no need for a super keyword.
class Display { public void display(){ System.out.println("display method of super class."); } } class Show extends Display { public void show(){ System.out.println("show method of sub class."); //no need of super keyword here. display(); } } public class SuperExample5 { public static void main(String args[]){ //create Show class object. Show obj = new Show(); //method call obj.show(); } }
Output
show method of sub class. display method of super class.
3. super can be used to access immediate superclass instance variables.
If superclass and subclass have the same instance variables and that variable is called from subclass then the subclass instance variable will be referred to.
class Display { int num = 100; } class Show extends Display { int num = 200; public void show(){ //sub class instance variable will be referred. System.out.println("num = " + num); } } public class SuperExample6 { public static void main(String args[]){ //create Show class object. Show obj = new Show(); //method call obj.show(); } }
Output
num = 200
The above problem can be solved with the super keyword.
class Display { int num = 100; } class Show extends Display { int num = 200; public void show(){ //super class instance variable will be referred. System.out.println("num = " + super.num); } } public class SuperExample7 { public static void main(String args[]){ //create Show class object. Show obj = new Show(); //method call obj.show(); } }
Output
num = 100
If sub-class and super-class instance variables are not the same then there is no need for a super keyword.
class Display { int num = 100; } class Show extends Display { public void show(){ //super class instance variable will be referred. System.out.println("num = " + num); } } public class SuperExample8 { public static void main(String args[]){ //create Show class object. Show obj = new Show(); //method call obj.show(); } }
Output
num = 100
Difference between this and the super keyword in Java.
- this is used for accessing variables and methods of the current class. super is used for accessing variables and methods of the immediate superclass.
- this is used in constructor chaining in the current class. super is used in constructor chaining in the immediate superclass.