Tutorials

Java Keywords


Java super keyword


Super keyword in java is used to call:

  • Immediate super-class instance variables
  • Immediate super-class methods
  • Immediate super-class constructors

We will learn about each one of them with proper example in this chapter.

 

Access super-class instance variables

When a class inherits another class then it may declare a variable whose name resembles to any of the instance variable of its immediate super class.

This is known as variable overriding.

Within the class local variable will be easily accessible. But what if you wanted to access the variable in the super-class which this class’s local variable overrides.

 

Super keyword comes as a savior.

See the example given below:


package inheritance;

public class SuperExample1 {

      public static void main(String[] args) {

            B b = new B();
            b.printVariables();

      }

}



class A {

      int x = 10;

}



class B extends A{

      int x = 5;  //x is overriden

      public void printVariables(){

            System.out.println("x in class B="+ x);
            System.out.println("x in class A="+ super.x); 

      }

}

OUTPUT:

x in class B=5
x in class A=10

 

How the above code snippet works?

In the above code snippet, it can be seen that variable x is overriden in the sub-class B. 

When you write x in class B, then you are referring to variable x declared in B

When you write super.x in class B, you are referring to variable x declared in super-class A.  

 

 

Access super-class methods

When a class inherits another class then it may declare a method whose name resembles to any of the method of its immediate super class.

This is known as method overriding.

If you wanted to access the method in the super-class which is overriden, then you need to use super keyword.

See the code snippet below:

 

package inheritance;

public class SuperExample2 {

public static void main(String[] args) {
BP bp = new BP();
bp.printVariables();

}

}

class AB {
public void printVariables(){
System.out.println("Method of class AB");
}
}

class BP extends AB{

public void printVariables(){  //overriden method
System.out.println("Method of class BP");
super.printVariables();  //call super class method
}
}

OUTPUT:

Method of class BP
Method of class AB

 

How the above code works?

In the above code snippet, it can be seen that method printVariables is overriden in the sub-class B. 

When you write super.printVariables(), you are referring to method printVariables declared in super-class A. And thus superclass method is called and is executed.

 

 

Access super-class constructors

Constructor cannot be overriden as it is never inherited.

When we inherit any class then we can also have the privilege to call its super class constructor.

We can do so by using super().

 

Please see the code snippet given below:

 


package inheritance;

public class SuperExample3 {

      public static void main(String[] args) {

            BPQ bp = new BPQ();

      }

}



class ABC {

      public ABC(){

            System.out.println("Constructor of class ABC");

      }

}



class BPQ extends ABC{

      public BPQ(){

            super();  //call super class method
            System.out.println("Constructor of class BPQ");

      }

}

OUTPUT:

Constructor of class ABC
Constructor of class BPQ

 

super() will call immediate super-class constructor.

 

 

 



Please Share this page
Views : 132
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!