super keyword in java

What is Super Keyword in Java

Super keyword in java is a type of reference variable that 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 a 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 the 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

Check out My Latest post on Developer Helps for some Interesting Insights


What is the Difference Between super and super() in Java

super() in Java is always used by the user when he wishes to make constructor calls. We use it to call the constructor of the base class. super() can only be called inside a constructor. It is not possible for the user to call super() anywhere else in the program. Super without the method is simply an instance of the super() class constructor. Hence the arguments in super are generated without any hassle. It can invoke any method which is accessible by super().

The syntax super.x will give the user access to all the methods and properties of the superclass. However, super() will simply you the instance of the class just like a constructor.

Summary:

  • The Super keyword removes 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.

Check out My Latest post on Developer Helps for some Interesting Insights


Leave a comment

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

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!