Java Tutorial

Super keyword

Super keyword is used for differentiating the base class features with derived class features. Super keyword is placing an important role in three places. They are at variable level, at method level and at constructor level.

Super at Variable level

Whenever we inherit the base class members into derived class, there is a possibility that base class members are similar to derived class members.

In order to distinguish the base class members with derived class members in the derived class, the base class members will be preceded by a keyword super.

Syntax for super at Variable level:

super. base class member name

For example:

class Bc
{
    int a;
};

class Dc extends Bc
{
    int a;
    void set (int x, int y)
    {
        super.a=x;
        a=y; //by default 'a' is preceded with 'this.' since 'this.' represents current class
    }
    void sum ()
    {
        System.out.println ("SUM = "+(super.a+a));
    }
};

class InDemo
{
    public static void main (String k [])
    {
        int n1=Integer.parseInt (k[0]);
        int n2=Integer.parseInt (k[1]);
        Dc do1=new Dc ();
        do1.set (n1, n2);
        do1.sum ();
    }
};

Super at Method level

Whenever we inherit the base class methods into the derived class, there is a possibility that base class methods are similar to derived methods.

To differentiate the base class methods with derived class methods in the derived class, the base class methods must be preceded by a keyword super.

Syntax for super at Method level:

super. base class method name

For example:

class Bc
{
    void display ()
    {
        System.out.println ("BASE CLASS - DISPLAY...");
    }
};

class Dc extends Bc
{
    void display ()
    {
        super.display (); //refers to base class display method
        System.out.println ("DERIVED CLASS - DISPLAY...");
    }
};

class InDemo
{
     public static void main (String k [])
    {
        Dc do1=new Dc ();
        do1.display ();
    }
};

Super at constructor level

Whenever we develop any inheritance application, we use to create always object of bottom most derived class. When we create an object of bottom most derived class, it in turns calls its immediate super class default constructor and it in turns calls its top most super class default constructor. Therefore, in JAVA environment, constructors will be called always from bottom to top and the execution starts from top to bottom.

Consider the following multi level inheritance:

Multi level inheritance

Super () is used for calling super class default constructor from default constructor or from parameterized constructor of derived class. It is optional.

Super (...) is used for calling super class parameterized constructor either from default constructor or from parameterized constructor of derived class. It is always mandatory.

Rules:

Whenever we use either super () or super (...) in derived class constructors they must be used as first statement.

supar rules

1. Whenever we want to call default constructor of base class from default constructor of derived class using super () in default constructor of derived class is optional.

For example:

class Bc
{
    Bc ()
    {
        System.out.println ("I AM FROM BASE CLASS...");
    }
};

class Ibc extends Bc
{
    Ibc ()
    {
        System.out.println ("I AM FROM INTERMEDIATE BASE CLASS...");
    }
};

class Dc extends Ibc
{
    Dc ()
    {
        super (); //optional
        System.out.println ("I AM FROM DERIVED CLASS...");
    }
};
 
class InDemo3
{
    public static void main (String k []) 
    {
        Dc o1=new Dc ();
    }
};

2. Whenever we want to call the super class parameterized class from parameterized class of the derived class using super (...) in parameterized class of derived class is mandatory.

For example:

class C1
{
    int a;
    C1 (int a)
    {
        System.out.println ("PARAMETERIZED CONSTRUCTOR - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};

class C2 extends C1
{
    int b;
    C2 (int a, int b)
    {
        super (a);
        System.out.println ("PARAMETERIZED CONSTRUCTOR - C2");
        this.b=b;
        System.out.println ("VALUE OF b = "+b);
    }
};


class InDemo4
{
    public static void main (String k [])
    {
        C2 o2=new C2 (10, 20);
    }
};

3. Whenever we want to call default constructor of base class from parameterized class of derived class using super () in parameterized class of derived class is optional.

For example:

class C1
{
    int a;
    C1 ()
    {
        System.out.println ("PARAMETERIZED CONSTRUCTOR - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};
 
class C2 extends C1
{
    int b;
    C2 (int b)
    {
        super (); //optional
        System.out.println ("PARAMETERIZED CONSTRUCTOR - C2");
        this.b=b;
        System.out.println ("VALUE OF b = "+b);
    }
};
 
class InDemo5
{
    public static void main (String k [])
    {
        C2 o2=new C2 (20);
    }
};

4. Whenever we want to call parameterized class of base class from default constructor of derived class using super (...) in default constructor of derived class is mandatory.

For example:

class C1
{
    int a;
    C1 (int a)
    {
        System.out.println ("PARAMETERIZED CONSTRUCTOR - C1");
        this.a=a;
        System.out.println ("VALUE OF a = "+a);
    }
};

class C2 extends C1
{
    int b;
    C2 ()
    {
        super (10);
        System.out.println ("DEFAULT CONSTRUCTOR - C2");
        this.b=20;
        System.out.println ("VALUE OF b = "+b);
    }
};
 
class InDemo6
{
    public static void main (String k [])
    {
        C2 o2=new C2 ();
    }
};

Best example for the above given rules:

class Bc
{
    Bc ()
    {
        System.out.println ("BASE CLASS - DEFAULT CONSTRUCTOR");
    }
    Bc (int x)
    {
        this ();
        System.out.println ("BASE CLASS - PARAMETERIZED CONSTRUCTOR");
    }
};

class Ibc extends Bc
{
    Ibc ()
    {
        super (100);
        System.out.println ("INTERMEDIATE BASE CLASS - DEFAULT CONSTRUCTOR");
    }
    Ibc (int x)
    {
        this ();
        System.out.println ("INTERMEDIATE BASE CLASS - PARAMETERIZED CONSTRUCTOR");
    }
};

class Dc extends Ibc
{
    Dc ()
    {
        this (10);
        System.out.println ("DERIVED CLASS - DEFAULT CONSTRUCTOR");
    }
    Dc (int x)
    {
        super (10);
        System.out.println ("DERIVED CLASS - PARAMETERIZED CONSTRUCTOR");
    }
};

class StDemo
{
    public static void main (String k [])
    {
        Dc do1=new Dc ();
    }
};