The super keyword in Java is a reference variable used to refer to immediate parent class objects. Whenever you create an instance of a subclass, an instance of the parent class is created implicitly, which is referred to by the super keyword. It is primarily used to eliminate confusion between superclasses and subclasses that have methods or variables with the same name.
The super keyword serves three distinct purposes in Object-Oriented programming. Understanding these is vital for mastering Java Inheritance.
Used to access the data members (fields) of the parent class when the child class has fields with the same name.
Used to call the parent class's version of a method that has been overridden in the child class.
Used to call the constructor of the immediate parent class from the subclass constructor.
In cases where a subclass has an attribute with the same name as the superclass, the child's variable takes precedence. To access the parent's version, we use super.variableName.
Animal has a field color and Dog also has color, inside the Dog class: System.out.println(color); // Prints Dog's color System.out.println(super.color); // Prints Animal's color
When you override a method, you might still want to execute the parent's logic before adding your own. This is common in frameworks like Android or Spring where you extend base classes.
The super() keyword is used to call the parent class constructor. This is essential because the child object cannot exist without first initializing its parent parts.
If you don't manually type super() in your child constructor, the Java compiler automatically adds it for you as the first line. This ensures that the parent class is always initialized first.
this(), the call to super() must be the very first statement in the constructor. You cannot use both this() and super() in the same constructor.
While they look similar, they refer to different instances in memory:
| Feature | this Keyword | super Keyword |
|---|---|---|
| Reference | Refers to the current class instance. | Refers to the immediate parent instance. |
| Purpose | To resolve shadowing within a class. | To access parent members after overriding. |
| Usage | In constructors and instance methods. | In constructors and instance methods. |
| Static Context | Cannot be used in static methods. | Cannot be used in static methods. |
This example demonstrates how to pass data to a parent constructor and call overridden methods using super.
This is a common interview trap. Static methods are class-level and do not belong to any specific instance. Since super refers to the parent object instance, and no object exists in a static environment, Java forbids its use. If you try, the compiler will report an error: "non-static variable super cannot be referenced from a static context."
Q: What happens if the parent class doesn't have a default (no-arg) constructor?
A: The compiler will throw an error in the subclass. In this case, you must manually call super(parameters) to match whatever constructor the parent class actually has.
Q: Can we use super.super.methodName()?
A: No. Java does not allow "Grandparent" access. You can only access the immediate parent class. This is part of Java's design to keep encapsulation clean.
Q: Is 'super' used for interfaces?
A: Not in the traditional sense of constructors (since interfaces don't have them). However, since Java 8, you can use InterfaceName.super.methodName() to call default methods from an interface.
The super keyword is the foundation of constructor and method chaining in Java. It ensures that hierarchies remain synchronized and that parent logic is respected even when a child class evolves. Mastering super is non-negotiable for anyone looking to build professional, scalable Java frameworks.