Java-Super to Call Superclass Constructor

Super to Call Superclass Constructor

We shall execute the constructor of a super class by using the super keyword in a simple command of the following form :

Here, the arguments represent an optional list of arguments for the constructor of the super class. The above statement should appear as the very first statement of the constructor of the subclass. If a subclass constructor does not call the super class constructor, the super class object is constructed with its default constructor. If the super class does not have a default constructor, then the compiler will report an error.



Program Source

class Super {
    int data1;
    int data2;
    Super(int a ,int b)
        data1 = a;
        data2 = b;
        System.out.println("Super class constructor");

class Sub extends Super{
    Sub(int a,int b)
        System.out.println("Sub class constructor");

public class Javaapp {

    public static void main(String[] args) {
        Sub obj = new Sub(20,40);
        System.out.println("obj.data1 = "+obj.data1);
        System.out.println("obj.data2 = "+obj.data2);

One thought on “Java-Super to Call Superclass Constructor

Leave a Comment