Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Constructor inheritance and use of Super keyword to access superclass constructor

Posted By: Mercedes Andrews     Category: Java     Views: 20854

This article explains about constructor inheritance and use of super keyword for constructors in java with examples.

Class can not only be determined by state and behavior of class but also by each of its superclasses. Due to that reason it is not sufficient to execute a constructor of that class but also need to initialize each constructor of superclasses.

A superclass constructor must execute before a subclass constructor. So that the state and behavior defined by the superclass may be correctly and completely initialized before a subclass constructor executes.

Java compiler assumes that the first line of every constructor is an implicit call to the default superclass constructor unless you explicitly use super() or this().

Note that Super keyword is used to explicitly invoke a superclass constructor. If you use this form, it must appear as the first statement of the constructor to ensure that the superclass constructor executes before the subclass constructor in java.

 

Syntax to call Superclass Constructor

super(args);  

args is an optional list of arguments of the superclass constructor.

Note that you cannot use both super() and this() at the same time in same constructor because both must appear as the first statement of the constructor.

 

Example of Constructor Inheritance and use of Super keyword to call Constructor of super class

Example 1 : Program that shows the default execution of constructor when classes are inherited

class Person
{
     Person()
     {
              System.out.println("Constructor of person class");
      }

class Employee extends Person
{
     Employee()
     {
              System.out.println("Constructor of employee class");
      }
}
 
class PermanentEmployee extends Employee
{
     PermanentEmployee()
     {
              System.out.println("Constructor of permanent employee class");
      }
}
 
class ConstructorInheritanceDemo
{
      public static void main(String args())
      {
               PermanentEmployee pObj = new PermanentEmployee();
       }
}
 
 
Output
 
Constructor of person class
Constructor of employee class
Constructor of permanent employee class

 

Note that the superclass person and Employee constructors executes before the subclass PermanentEmployee.

 

Example 2 : Program that illustrates the explicit use of super() in an inheritance hierarchy.

class Person
{
     String FirstName;
     String LastName;
 
     Person(String fName, String lName)
     {
              FirstName = fName;
              LastName = lName;
      }

class Student extends Person
{
     int id;
     String standard;
     String instructor;
 
     Student(String fName, String lName, int nId, String stnd, String instr)
     {
          super(fName,lName);
          id = nId;
          standard = stnd;
          instructor = instr;         
      }
}


class SuperKeywordForConstructorDemo
{
       public static void main(String args[])
       {
               Student sObj = new Student("Jacob","Smith",1,"1 - B","Roma");
 
               System.out.println("Student :");
               System.out.println("First Name : " + sObj.FirstName);
               System.out.println("Last Name : " + sObj.LastName);
               System.out.println("ID : " + sObj.id);
               System.out.println("Standard : " + sObj.standard);
               System.out.println("Instructor : " + sObj.instructor);
        }
}
 

Output
 
Student :
First Name : Jacob
Last Name : Smith
ID : 1
Standard : 1 - B
Instructor : Roma
 
  
Share: 



Mercedes Andrews
Mercedes Andrews author of Constructor inheritance and use of Super keyword to access superclass constructor is from Chicago, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!