Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – The super keyword

The super keyword is used to indicate the superclass of the class in which it resides.

It can be used in two ways

i) To access a suppressed member (instance variable or method) of the superclass.
ii) To call a constructor of the superclass.

i) To access a suppressed member (instance variable or method) of the superclass.

If a superclass and a subclass have a member with the same name, then in a subclass, the member of subclass always dominates over superclass member.

Example

class Father 
{
    int bankBalance = 500000;
}

class Daughter extends Father
{
    int bankBalance = 300000;
	
    public void displayBalance()
    {
        System.out.println("Bank balance = "+bankBalance);		
    }
}

class WithoutSuper 
{
    public static void main(String args[])
    {
        Daughter d = new Daughter();
        d.displayBalance();		
    }
}

Output

Bank balance = 300000

Here every time we run the program we get the bank balance of daughter only and not of Father even though it is inherited by a Daughter subclass.

It happens because bankBalance of class Daughter hides (dominates over) the bankBalance of class Father.

But if we want to check the bankBalance of class Father using the same code above, we just need to do a small change in checkBalance() method of a class Daughter.

Instead of writing System.out.println(bankBalance) we should write System.out.println(super.bankBalance).

That is

super.memberName

Example

class Father 
{
    int bankBalance = 500000;
}

class Daughter extends Father
{
    int bankBalance = 300000;
	
    public void displayBalance()
    {
        System.out.println("Bank balance = "+super.bankBalance);		
    }
}

class WithSuper 
{
    public static void main(String args[])
    {
        Daughter d = new Daughter();
        d.displayBalance();		
    }
}

Output

Bank balance = 500000

For a better understanding of the above program look at the diagram shown below:

Java Programming Language super Keyword to Access a Suppressed Member of the superclass

This is how the super keyword is used to access a member of the superclass which is hidden by a member of the subclass, as shown in the above program.

ii) To call a constructor of the superclass

Using super keyword, we can pass arguments to superclass constructor from its subclass constructor.

Example

class A 
{
    int i;
    int j;
	
    A(int x,int y)
    {
        System.out.println("In parameterized constructor of class A");
        i = x;
        j = y;
    }
}

class B extends A
{
    int k;
	
    B(int a,int b,int c)
    {
        super(a,b);
        System.out.println("In parameterized constructor of class B");
        k = c;
    }
	
    public void display()
    {
        System.out.println("i = "+i);
        System.out.println("j = "+j);
        System.out.println("k = "+k);
    }
}

class Demo 
{
    public static void main(String args[])
    {
        B b = new B(10, 20, 30);
        b.display();		
    }
}

Output

In parameterized constructor of class A
In parameterized constructor of class B
i = 10
j = 20
k = 30

For a better understanding of the above program look at the diagram shown below:

Java Programming Language super Keyword to Call a Constructor of the superclass

This is how the super keyword is used to pass arguments to superclass constructor from subclass.

NOTE

The super() must always be used at the starting of the subclass’ constructor.

 

Leave a reply