constructor references

Super at constructor level

The super keyword can also be used to invoke or call the parent class constructor. Constructor are calling from bottom to top and executing from top to bottom.
To establish the connection between base class constructor and derived class constructors JVM provides two implicit methods they are:
  • Super()
  • Super(...)

Super()

Super() It is used for calling super class default constructor from the context of derived class constructors.

Super keyword used to call base class constructor

Syntax

class Employee
{
Employee()
{
System.out.println("Employee class Constructor");
}
}
class HR extends Employee
{
HR()
{
super(); //will invoke or call parent class constructor  
System.out.println("HR class Constructor");
}
}
class Supercons
{
public static void main(String[] args)
{
HR obj=new HR();
}
}

Output

 
Employee class Constructor
HR class Constructor

No comments:

Post a Comment