Super Keyword in Java: Syntax, Uses and Examples

Super keyword in Java is a reference variable that refers to an immediate superclass object of the current object. The keyword ‘super’ comes into the picture with the concept of inheritance in Java.

The super keyword always represents a superclass object. When you create an object of the subclass, an object of superclass is created implicitly, which is referred by the super reference variable. It does not create a new object but points to the inherited members of the parent class.

The keyword “super” allows you to access members of a superclass in a subclass. If you want to call members of the superclass explicitly from a subclass, then use the super keyword.

Where to Use Super Keyword in Java?


You can apply super keyword with variables, methods, and constructors of the parent class. Thus, the super keyword in Java helps to call immediate data members or member functions of the parent class.

Not only that, you can also call a constructor of superclass explicitly from a constructor of subclass. JVM always uses super() to call the default constructor of superclass implicitly from the subclass constructor.

Uses of Super Keyword in Java


You can use the super keyword to access members of an immediate superclass from a subclass. The main purpose of using super keyword in Java:

  • To resolve ambiguity when superclass and subclass members have the same name.
  • To access hidden variables of the immediate parent class.
  • To call overridden methods of the superclass explicitly.
  • To invoke constructors of the immediate superclass.

Let us understand each of these points with the help of examples.

Usage of super keyword in Java

Use of Super Keyword with Variables


You can use the “super” keyword to access (or call) a hidden instance variable of the superclass in the subclass. When you declare an instance variable with the same name in the subclass as defined in the superclass, the superclass variable becomes hidden, not overridden.

Because of this name conflict, you cannot directly access superclass variable using its name inside the subclass. To resolve this issue, we use super keyword to explicitly refer to superclass variable from the subclass.

Syntax to Access Superclass Variable Using Super

The general syntax to call superclass instance variable in the subclass using super keyword is:

super.variable_name;

For example:
     super.a;  // Here, a is an instance variable.

The super.variableName only accesses superclass instance variables. It works only with the non-static context. You cannot apply super with static context.

Example: Accessing Superclass Instance Variable Using super

[blocksy-content-block id=”12371″]
Let us take an example program in which we will call superclass instance variable from the subclass using super keyword.

package superKeyword; 
class SuperClass 
{ 
 // Declare an instance variable and assign a value to it. 
    int x = 100; 
} 
public class SubClass extends SuperClass 
{ 
 // Declare an instance variable with the same name as in its superclass. 
    int x = 200; 
  
    void display() 
    { 
   // This statement accesses the instance variable of the current class SubClass.
   // In Java, an unqualified variable name refers to the current class variable.
      System.out.println("Value of variable of Sub: " +x);  
 

   // To access superclass instance variable, we will use the super keyword as a reference variable. 
      System.out.println("Value of variable of SuperClass: " +super.x); // x of class SuperClass will call. 
   } 
   public static void main(String[] args) 
   { 
   // Create an instance of SubClass.
      SubClass s = new SubClass();
   // Call the display() method using reference variable s. 
      s.display(); 
   } 
}

Output:

Value of variable of SubClass: 200 
Value of variable of SuperClass: 100

In this example:

  • We have created a superclass named SuperClass, which contains an instance variable x with a value of 100.
  • Then, we created a subclass named SubClass, which extends SuperClass.
  • The subclass declares its own instance variable x with a value of 200.
  • Since the subclass instance variable has the same name as in its superclass, this causes variable hiding.
  • Inside display() method, we have accessed the subclass instance variable x, which refers to SubClass’s variable (this.x).
  • Also, we have used super.x to access the variable of SuperClass parent class inside the child class. This superclass variable has hidden inside the subclass because of the same name.

This is a perfect example of accessing a hidden superclass instance variable when ambiguity arises due to the same variable name in both superclass and subclass.

Use of Super with Constructor in Java


We know a constructor creates an instance of a class. The constructor of superclass does not inherit into the subclass. Therefore, it can only be called from the subclass constructor using the keyword super.

In other words, we can invoke immediate superclass constructor from within subclass constructor using super keyword. If you don’t use the super keyword to call superclass constructor in any class’s constructor, Java compiler will automatically put the super at the first line in the class’s constructor to call superclass constructor.

Let us consider some valid and invalid examples of calling the superclass constructor using the super keyword in Java.

Example 1: Valid

class A
{ 
 // Default constructor put by Java compiler during the compilation.
    public A() {   
       // invisible super(); present here.
    }
} 

Java compiler inserts super automatically. So, this is valid example.

Example 2: Valid

class X
{
   public X( )
   {
      System.out.println("Hello Java");
      super( ); // Error because super must be at first line of constructor.
   }
}

The super() must be the first statement inside the subclass constructor. Otherwise, the compiler will generate compile-time error.
[blocksy-content-block id=”12121″]

Example 3: Invalid Use

class ChildClass extends ParentClass {
void msg( ) 
   {
       super( );  // Compile-time error: super() cannot be used inside a method.
   }
}

Constructor calls using super() are allowed only inside a constructor. A method cannot call a constructor. Therefore, using super() inside a method results in a compile-time error.

Example 4: Invalid Use

class ChildClass extends ParentClass {

  ChildClass(int a) {
     super();      // Valid: calls no-argument constructor of Parent
    // super(10);  // Invalid: only one constructor call is allowed
  }
}

Note that a constructor can invoke only one constructor using either super() or this(), and it must be the first statement in the constructor body. Calling super() and super(10) together causes a compile-time error.

Syntax to call Superclass Constructor

The general syntax to call a superclass constructor using super keyword in Java is as:

super();
Or, 
super(arguments);

In the above syntax:

  • The statement super() calls no-argument constructor of its superclass.
  • The statement super(arguments) calls parent class constructor that matches arguments. The subclass constructor passes arguments to superclass constructor using the super keyword.
  • The statement super() or super(arguments) must be the first line of child class constructor. Calling a parent class constructor’s name in the child class causes syntax error.
  • A subclass does not inherit the constructors of its superclass. It can only be called from the constructor of subclass using the keyword super.
  • When you create an object of a class, its constructor is invoked automatically. Internally, constructor calls superclass constructor with the help of super keyword. This process refers to constructor chaining in Java.
  • If a class extends another class, it is better to use a no-argument constructor to make the class easier to extend and to avoid compilation errors in the subclasses.
  • There is not any super.super in Java. It is illegal.

Example 5:

package superKeyword; 
class Fruit 
{ 
// Here, the class Fruit has defined an explicit constructor.
   public Fruit(int x) 
   { 
      System.out.println("Explicit fruit constructor defined"); 
   } 
 } 
public class Apple extends Fruit { 
   public static void main(String[] args) 
   { 
  // Create an instance of class Apple.
     Apple a = new Apple(); 
   } 
}

In the above example code:

  • There is no explicitly defined constructor in class Apple. Therefore, Java compiler will implicitly add a default no-argument constructor in the class Apple because we have not defined any constructor explicitly in the Apple class.
  • Since Apple is the subclass of Fruit class, it will automatically call no-argument constructor of class Fruit.
  • However, Fruit does not have a no-argument constructor because we have defined an explicit constructor.

[blocksy-content-block id=”12153″]
Remember that Java compiler adds a default constructor only when you do not define any constructor explicitly in the class. Therefore, the program cannot be compiled if the superclass does not have an accessible no-argument constructor.

To compile the above code, explicitly declare a no-argument constructor in the Fruit class.

package superKeyword; 
class Fruit 
{ 
// Declare a no-argument constructor. 
   Fruit() 
   { 
      System.out.println("Fruit no-argument constructor"); 
   } 
// Fruit has defined an explicit constructor. 
   public Fruit(int x) 
   { 
     System.out.println("Fruit constructor"); 
   } 
} 
public class Apple extends Fruit { 
   public static void main(String[] args) 
   { 
      Apple a = new Apple(); 
   } 
}

Output:

Fruit no-argument constructor

Example 6: Constructor Chaining Using Super

package superKeyword; 
class P 
{ 
   P() 
   { 
    // Invisible super(); present here.
       System.out.println("P class's no-arg constructor"); 
   } 
   P(int x) 
   { 
      System.out.println("P class's one argument constructor"); 
   } 
   public void show() 
   { 
      System.out.println("P class's method"); 
   } 
} 
class Q extends P 
{ 
  Q() 
  { 
    // super(); // Java compiler automatically added super at compile-time.
       System.out.println("Q class's no-arg constructor"); 
   } 
 } 
public class Test { 
public static void main(String[] args) 
{ 
  // Create an object of class Q and call the method show() using reference variable obj. 
     Q obj = new Q(); 
     obj.show(); 
 } 
}

Output:

P class's no-arg constructor 
Q class's no-arg constructor 
P class's method

In this example:

  • When the JVM executes the statement Q obj = new Q();, the constructor of class Q is invoked.
  • Inside class Q, by default, Java compiler put super keyword as the first statement at compile-time.
  • The super() call invokes the no-argument constructor of the superclass P. Therefore, the first output is “P class’s no-arg constructor”.
  • After the complete execution of superclass no-arg constructor, the control of execution returns to the constructor Q() of class Q. Hence, the second output is “Q class’s no-arg constructor”.
  • When obj.show(); will execute, the show() method defined in class P is called because this method is available by default in class Q through inheritance. Therefore, the third output is “P class’s method”.

Use of Super with Method in Java


The keyword super can also be used to refer to a superclass method, in addition to calling a superclass constructor. If a method of the subclass overrides one method of its superclass, the overridden method can be called through the use of super keyword.

In other words, you can use super when a subclass method overrides a method and wants to invoke the method defined inside the superclass.

Syntax to Call Superclass Method

The general syntax to call a superclass method is as follows:

super.method_name()
super.method_name(parameters);

For example:
  super.msg();

Let’s take an example program based on the concept of calling superclass method in Java using super keyword.

Example: Calling Superclass Method Using Super

package superKeyword; 
class Student 
{ 
 // Overridden method. 
    void displayInfo() 
    { 
       System.out.println("I am John"); 
    } 
} 
public class School extends Student 
{ 
 // Overriding method. 
    void displayInfo() 
    { 
       System.out.println("My school's name is RSVM"); 
    } 
    void msg() 
    { 
       super.displayInfo(); // displayInfo() of class Student is called. 
       displayInfo(); // displayInfo of class School is called. 
    } 
    public static void main(String[] args) 
    { 
    // Create an instance of subclass School.
       School sc = new School(); 
       sc.msg(); 
    } 
}

Output:

I am John 
My school's name is RSVM

In this example:

  • The parent class ‘Student’ and child class ‘School’ both have a displayInfo() method. Since the method of child class overrides the method ‘displayInfo’ of its parent class, method overriding occurs.
  • If you call displayInfo() method by using reference variable sc, it will call displayInfo() method of child class, not of parent class. This is because the method that gets executed is determined at runtime based on the actual object type.
  • The super.displayInfo(); invokes the displayInfo() method defined in the parent class Student.

Question:

Suppose class X extends class Y, Y extends class Z, and a method msg() is defined in class Z. Can you invoke super.super.msg() from class X?

Answer: The answer is no. It is illegal to have such a chain of supers in Java.

Advantages of Super Keyword in Java


There are several advantages of using super keyword in Java programming. They are:

  • The main advantage of using super keyword is that it allows a subclass to access members (variables and methods) of its superclass. This is especially useful when both the superclass and subclass have members with the same name. By using super, we can explicitly refer to the superclass’s members.
  • When a subclass constructor is invoked, the super() call is automatically added by the Java compiler if no explicit constructor call is made. This facilitates constructor chaining.
  • When we override a method in a subclass, the super keyword is often used to call the overridden method of the superclass. It enables us to leverage the existing functionality of the superclass while adding specific functionality in the subclass.
  • We can use the super keyword to call specific constructors of the superclass. This is especially helpful when the superclass has multiple constructors with different parameter lists. By using super(arguments), we can choose which constructor to call.
  • If a subclass and its superclass have members with the same name, using super helps in resolving ambiguity by explicitly indicating that we want to access the superclass’s member.

Disadvantages of Super Keyword in Java


In addition to some advantages of using super keyword in Java, there are also some disadvantages of super keyword:

  • You cannot use super outside a subclass because it has no meaning in a class that does not extend another class.
  • If you use the super inside a constructor, it must appear as the first line. This restriction can limit flexibility in constructor logic.
  • The super keyword cannot access private variables or methods of the superclass. Only protected or public members are accessible.
  • Java does not allow using super.super to call grandparent class members directly, which can sometimes limit deep inheritance access.
  • Super keyword only works in inheritance. If a class does not extend another class, super has no use.

Conclusion

The super keyword in Java is a powerful feature that allows subclasses to access superclass variables and methods, call overridden methods, and invoke specific superclass constructors. However, it comes with some limitations, such as restricted access to private members.

We hope that you have learned how to call instance variables, constructors, and methods of superclass using super keyword. Stay tuned with the next tutorial where you will learn this keyword in Java in easy words.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.