«Back to Home

Core Java

Topics

Super Keyword In Java

Super Keyword

In Java, “Super” keyword is a reference variable, which is used to refer current parent class object.
 
Whenever we create the instance of subclass at the same time, an instance of parent class is also created implicitly by JVM.
 
Usage of “super” keyword in Java
  • “Super” keyword is used to refer the current parent class instance variable.

  • Super () keyword is used to call the current parent class constructor.

  • Super keyword is used to call the current parent class method.
Super keyword is used to refer current parent class instance variable.

In Java, “super” keyword is used to refer current parent class instance variable.
 
Let’s see an example, given below.
 
Code
  1. class Dog {  
  2.     int age = 20;  
  3. }  
  4. public class BabyDog extends Dog {  
  5.     int age = 5;  
  6.     void displayAge() {  
  7.         System.out.println(super.age); // it will print age of Dog    
  8.     }  
  9.     public static void main(String args[]) {  
  10.         BabyDog b = new BabyDog();  
  11.         b.displayAge();  
  12.     }  
  13. }  
23
 
Output

24

In the example, mentioned above, Dog and BabyDog both class have a common property age. Instance variable of current (child) class is refered bydefault, but we have to refer parent class instance variable due to which we use super keyword to differentiate between parent class instance variable and current class instance variable.
 
If we don’t use super keyword in program, let’s see an example, given below.
 
Code
  1. class Dog {  
  2.     int age = 20;  
  3. }  
  4. public class BabyDog extends Dog {  
  5.     int age = 5;  
  6.     void displayAge() {  
  7.         System.out.println("Age :" + age); // it will print age of BabyDog    
  8.     }  
  9.     public static void main(String args[]) {  
  10.         BabyDog b = new BabyDog();  
  11.         b.displayAge();  
  12.     }  
  13. }  
25

Output

26
 
Super () keyword is used to call current parent class constructor.

In Java, super () keyword is also used to call current parent class constructor.
 
Let’s see an example, given below.
 
Code
  1. class Dog {  
  2.     int age = 20;  
  3.     Dog() {  
  4.         System.out.println("Dog is a Animal");  
  5.     }  
  6. }  
  7. public class BabyDog extends Dog {  
  8.     int age = 5;  
  9.     BabyDog() {  
  10.         super(); // it will call parent class constructor  
  11.         System.out.println("BabyDog is a small Animal");  
  12.     }  
  13.     void displayAge() {  
  14.         System.out.println("Age :" + age); // it will print age of BabyDog    
  15.     }  
  16.     public static void main(String args[]) {  
  17.         BabyDog b = new BabyDog();  
  18.         b.displayAge();  
  19.     }  
  20. }  
27
 
Output

28

Super() is added in each and every class constructor automatically by compiler.


65
 
In the example, shown above, we create a constructor and use super keyword as the first statement to call a current parent class constructor. We know that the default constructor is provided by the compiler. We don’t have to need to write super ().
 
Super keyword is used to call current parent class method.

In Java, super keyword is also used to call parent class method.
 
Let’s see an example, given below.
 
Code
  1. class Dog {  
  2.     void eat() {  
  3.         System.out.println("Dog eat veg and nonveg both");  
  4.     }  
  5. }  
  6. public class BabyDog extends Dog {  
  7.     void eat() {  
  8.         System.out.println("BabyDog drink milk only");  
  9.     }  
  10.     void displayMsg() {  
  11.         eat(); //it will call current class eat() method    
  12.         super.eat(); // it will call parent class eat method    
  13.     }  
  14.     public static void main(String args[]) {  
  15.         BabyDog b = new BabyDog();  
  16.         b.displayMsg();  
  17.     }  
  18. }  
30
 
Output

31

In the example, shown above, Dog and BabyDog are both the classes, which have same method eat() and if we call eat method from BabyDog class, it will call the eat() method of BabyDog class and not of Dog class because it gives priority to local class. In case, there is no method in subclass as parent, there is no need to use super.
 
Let’s see an example, given below, if there is no method in subclass as parent.
 
Code
  1. class Dog {  
  2.     void eat() {  
  3.         System.out.println("Dog is parent of BabyDog child");  
  4.     }  
  5. }  
  6. public class BabyDog extends Dog {  
  7.     void displayMsg() {  
  8.         eat(); //it will call parent class eat() method    
  9.     }  
  10.     public static void main(String args[]) {  
  11.         BabyDog b = new BabyDog();  
  12.         b.displayMsg();  
  13.     }  
  14. }  
32

Output

33
 
In the example, shown above, eat() method is call from BabyDog class but BabyDog class does not have eat() method, Hence, we can call Dog class eat() method directly.
 
Summary
 
Thus, we learnt that “super” keyword is a reference variable, which is used to refer the current parent class object and also learnt how to use it in Java.