super constructor – JAVA

super constructor provides us the facility to call a constructor of a super class from within the constructor of derived class.

Call to the super constructor must always be the first statement inside the constructor.

When we use super constructor to call the constructor of super class with matching arguments, the by default call to the default constructor of the super class gets overridden by the specified call to the super constructor.

//program to show use of super Constructor

class Base

{

Base()

{

System.out.println(“Base() invoked”);

}

Base(int a)

{

System.out.println(“Base(int a) invoked”);

}

}

class Derived extends Base

{

Derived()

{

System.out.println(“Derived() invoked”);

}

Derived(int a)

{

super(a);

System.out.println(“Derived(int a) invoked”);

}

public static void main(String args[])

{

Derived ob=new Derived(11);

}

}

Leave a Reply

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