image

Understanding Keyword Super: Java Tutorial

The super keyword in Java is used to call superclass members such as methods and constructors. It provides a way to invoke members of the superclass from within it. In this tutorial, we will explore the various use cases of the super keyword and gain a deeper understanding of its functionality.

Accessing Superclass Members

One of the primary uses of the super keyword is to access members (methods and variables) of the superclass from within the subclass. This is particularly useful when you want to call a method of the superclass that the subclass has overridden or when you want to access a variable from the superclass that has been shadowed by a variable with the same name in the subclass.

class SuperClass {
    int x = 10;
    void printX() {
        System.out.println("Value of x in SuperClass: " + x);
     }
}
class SubClass extends SuperClass {
     int x = 20; // Shadows the variable x from SuperClass
     void printX(){
        System.out.println("Value of x in SubClass: " + x); // Prints 20                   
        System.out.println("Value of x in SuperClass: " + super.x); // Prints 10           
        super.printX(); // Calls the printX() method from SuperClass
     }
}

In the above example, the super the keyword is used to access the variable x and the method printX() from the SuperClass within the SubClass.

Calling Superclass Constructors

The super a keyword can also be used to call a superclass constructor from within a subclass constructor. This is necessary to initialize the superclass members or perform some operations in the superclass constructor before executing the subclass constructor.

class SuperClass
     SuperClass() {
         System.out.println("SuperClass constructor called");
     }
}
class SubClass extends SuperClass {
      SubClass() {
           super(); // Calls the SuperClass constructor                                       System.out.println("SubClass constructor called");
      }
}

In the above example, the super() call in the SubClass constructor invokes the constructor of the SuperClass. If you don't include this super() call explicitly, Java will automatically add a default super() call at the beginning of the subclass constructor.

Using super with Instance Initializer Blocks

Instance initializer blocks are blocks of code executed before the constructor of a class. The super a keyword can be used within these blocks to call a constructor or method of the superclass.

class SuperClass {
    SuperClass() {
         System.out.println("SuperClass constructor");
    }
    void printMessage() {
         System.out.println("Message from SuperClass");
    }
}
class SubClass extends SuperClass {
    {
         super.printMessage(); // Calling a method from SuperClass
     }
     SubClass() {
          super(); // Calling the SuperClass constructor                                      System.out.println("SubClass constructor");
     }
}

In the above example, the instance initializer block in the SubClass calls the printMessage() method from the SuperClass using the super keyword.

super vs this Keyword

While the super the keyword is used to access members of the superclass, the this keyword is used to access members of the current class. Understanding the difference between these two keywords and using them correctly in your code is important.

class SuperClass {
    int x;

    SuperClass(int x) {
         this.x = x; // Using 'this' to refer to the instance variable x
     }
}

class SubClass extends SuperClass {
     int y;

     SubClass(int x, int y) {
          super(x); // Calling the SuperClass constructor with x
          this.y = y; // Using 'this' to refer to the instance variable y
     }
}

In the above example, super(x) is used to call the constructor of the SuperClass with the x parameter, while this.y = y is used to assign the value of y to the instance variable y in the SubClass.

FAQs

Q1. Can we call the constructor of the superclass directly without using the super keyword?

No, you cannot call the superclass's constructor directly without using the super keyword. The super keyword is required to invoke a superclass constructor from within a subclass constructor.

Q2. Can we use the super keyword to access static methods or variables of the superclass?

No, the super keyword cannot be used to access static members of the superclass. Static members belong to the class itself, not to any specific instance. To access static members, you should use the class name directly.

Q3. What happens if we don't include a super() call in the subclass constructor?

If you don't include an explicit call  super() in the subclass constructor, Java will automatically insert a default super() call at the beginning of the subclass constructor. This default super() call invokes the no-argument constructor of the superclass.

Q4. Can we use the super keyword in a class that doesn't have a superclass?

No, the super a keyword can only be used in a subclass that extends another class. If a class doesn't have a superclass (i.e., it extends Object by default), using the super keyword will result in a compilation error.

Q5. Can we use the super keyword to access members of the current class?

No, the super keyword is specifically used to access members of the superclass. To access members of the current class, you should use the this keyword or refer to the members directly.

Share On