super keyword in java

Super keyword in Java

Super keyword in java is a type of reference variable which is used to refer to the parent class in java. It can also be called a superclass. The keyword is used to call the methods or constructors of the parent class. As we started using the concept of inheritance, the super keyword came into the picture. If we have methods of the same name in the parent and child class, then the super keyword is of utmost importance to avoid the same name confusion.

Refer to our basic OOPs concepts in Java for better understanding: Inheritance and polymorphism.

Super Keyword Uses in Java

  • Using it with variables: This situation occurs when the parent and child class have the same data members. We use the super keyword to remove the JVM ambiguity.
  • Using it with data members: we use super when we want to call the method of the parent class. As parent and child class will have the same data members, super will remove the ambiguity in that case.
  • Using it with constructors: super keyword can access both the parametric as well as non- parametric constructors of the parent class.
  • The super() method should be invoked initially in the child class constructor.
  • The keyword will override all the properties of the parent class.
  • In case we don’t wish to inherit the properties of the super class, we can use final keyword.

Below is a program for better understanding of how super class can be invoked using parent class method:

class Mammals{  
void eat(){
System.out.println("Mammals can eat");}  
}  
class Animal extends Mammals{  
void eat(){
System.out.println("Animals are also mammals");
}  
void bark(){
System.out.println("Animals also bark");}  
void work(){  
super.eat();  
bark();  
}  
}  
class Developerhelps{  
public static void main(String args[]){  
Animal m=new Animal();  
m.work();  
}} 

The output of this program will be:

Mammals can eat
Animals also bark

As we discussed the final keyword would help in not inheriting the properties. Below is a demonstration of how we can do this:

final class Mammal{
}
class Animal extends Mammal {
}

The above example will show an error: cannot inherit from class Mammal

Summary

  • The Super keyword to remove the JVM ambiguity in case parents and child class has the same data members.
  • Super class will automatically override the methods of the parent class.
  • The super() is always invoked in the child class, and not in the parent class. That’s how it inherits the properties. Β 
  • Subclass constructor has to call the parameterized constructor of the parent super class always.
  • Β As super inherits the properties of the parent class, it uses the methods and constructors for the reusability when we create a new class.
  • If a constructor is invoked in the subclass, it is automatic that the whole chain of constructors of the parent class. This process is also called the constructor chaining process.
  • JVM calls for a no-argument constructor of the super class when a constructor is explicitly or implicitly not called from the parent class. In that case, the coder gets a compile-time error as the output.

Leave a comment

Your email address will not be published. Required fields are marked *