Java Superclass Constructor

The OOPS concepts like inheritance and polymorphism have paved the way for the need for the super keyword. This keyword as we already know refers to the parent class. You can view our article on the super keyword to know the other functionalities of how it can be used. This article mainly focuses on the java superclass constructor part. 

Why super constructors?

During inheritance, we can access the parent class variables and methods using the super keyword. It can also access the parameterized and non-parameterized constructors present in the parent class. 

Calling a super constructor in java

A subclass can contain its own private data members, methods, and constructors. The constructors that belong to a subclass are limited and can initialize the variables that are present only inside the subclass.  

Once the object for the subclass is instantiated, it will automatically execute the constructors of the superclass. We can use the super keyword to call the superclass constructor. 

Here is a sample snippet for creating a SuperClass and extending it to another class:

class SuperClass{
   public void display() {
     
System.out.println("This is a sample method");
   }
}
public class SubClass extends SuperClass {
   public static void main(String args[]) {
      SubClass s = new SubClass();
     
s.display();
   }
}

In the above sample, SuperClass is the name of the superclass and SubClass is the name of the subclass. The method present in SuperClass is display() and it prints a message. 

Output:

This is a sample method.

Inheriting the java superclass constructor:

Basically, in inheritance, we do not actually inherit the constructor. We call them with the help of the super keyword. 

In the case of a parameterized constructor, the parameters are also taken within the constructor of the subclass. 

Here is a snippet of inheriting a constructor using the super keyword:

public Employee(String e_name, int e_age, String desig, int e_id){
   super(e_name, e_age);
   this.desig = desig;
   this.e_id = e_id;
}

Sample program to inherit a constructor using the super keyword:

class Employee{
   public String e_name;
   public int e_age;
   public Employee(String e_name, int e_age){
     
this.e_name = e_name;
      this.e_age = e_age;
   }
   public void displayEmployee() {
     
System.out.println("Details of the Employee class: ");
     
System.out.println("Name: "+this.e_name);
     
System.out.println("Age: "+this.e_age);
   }
}
public class Manager extends Employee {
   public String branch;
   public int Manager_id;
   public Manager(String e_name, int e_age, String branch, int Manager_id){
     
super(e_name, e_age);
     
this.branch = branch;
     
this.Manager_id = Manager_id;
   }
   public void displayManager() {
      System.out.println("Details of the Manager class: ");
     
System.out.println("Name: "+this.e_name);
     
System.out.println("Age: "+this.e_age);
     
System.out.println("Branch: "+this.branch);
     
System.out.println("Manager ID: "+this.Manager_id);
   }
   public static void main(String[] args) throws CloneNotSupportedException {
      Employee e = new Manager("Nisha", 26, "IT", 2369);
     
e.displayEmployee();
   }
}

Output:

Details of the Employee class:
Name: Nisha
Age: 26

Conclusion:

This is all about the java superclass constructor. I hope this article gave you a clear idea of this concept. You can try the sample programs to understand the working of the superclass constructor in Java. Also, refer to the article on the super keyword for more insights. 

Leave a Reply

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