- Super keyword refers to the parent class that is one level up in the hierarchy. To this end, gaining direct access to inherited members through subclasses and keeping the correct working of the inheritance chain is the main aim of using the super keyword by subclasses.
- When a child class redefines or conceals members of the parent class, the super keyword can still be used to refer to parent class variables, methods, and constructors.
- The super keyword changes (resolves) naming and behavior conflicts. Moreover, it helps to recognize the parent members when there is shadowing of variables or overriding of methods, i.e., child members are used in these cases.
- With the help of super keyword, it is possible to call the parent constructor first thus ensuring constructor chaining, which, in turn, is a prerequisite of stable object creation.
- Besides that, Java's encapsulation regulations are observed by the super keyword which cannot be used for accessing the private members of the superclass yet it still allows controlled access to the inherited features.
- Using the keyword super is a must for inheritance that is well organized, code reuse, and polymorphism as it is of great help to developers who want to extend base functionality without writing the same code twice or having an unclear situation.
Java's super keyword is a reference variable that helps you get members (variables, methods, or constructors) of a parent class that you have from a subclass. It is the primary mechanism for inheritance, guiding subclasses to use inherited members and resolve conflicts between overridden methods and variables. With super, programmers are able to call the methods of the parent class, get access to the hidden variables, and call the constructors for making sure child classes are properly initialized and work as expected.
The article is about different ways the super keyword can be used in Java with the help of which you can understand the significance of method overriding, constructor chaining, and dealing with inheritance hierarchies.
The super keyword is a type of reference that refers to the direct parent class object. It is used to call the parent class's methods, variables, and constructors. The super keyword is accessed primarily via inheritance to call inherited parent class members whenever overridden or hidden by the child class, with appropriate access to inherited behaviours and attributes.
Usage
There are three ways to use of super keyword in java:
- Use of super with Variables
- Use of super with Methods
- Use of super with Constructors
Here are the examples of super keyword in Java such as:
1. Use of super with Variables
The super keyword can be used to reference parent class instance variables in case the names of parent class members and child class members conflict.
Syntax
super.variableName;Example Code
Here is the Java program for super keyword using variables:
class Animal {
String sound = "Some sound";
}
class Dog extends Animal {
String sound = "Bark";
void printSound() {
System.out.println("Child class sound: " + sound);
System.out.println("Parent class sound: " + super.sound);
}
}
public class TestSuper {
public static void main(String[] args) {
Dog dog = new Dog();
dog.printSound();
}
}Explanation
- The Dog class inherits from the Animal class.
- Both classes have a sound variable, but the child class Dog overrides it.
- Using super.sound accesses the sound variable from the parent Animal class, while just sound refers to the variable from the child class.
Output
Child class sound: Bark
Parent class sound: Some soundKey Takeaways So Far
- The keyword super is used for resolution of shadowing of variables in the case when a parent and a child have variables with the same names.
- Thus it is guaranteed that the fields of the superclass will be accessed correctly.
- Supports clarity and comfort of reading by inheritance without any ambiguities.
- Helpful in situations when the functionality is being extended and the parent attributes are kept.
2. Use of super with Methods
The super keyword is used to call the parent class's method when the method is overridden in the child class.
Syntax
super.methodName();Example Code
Here is the java program for super keyword using methods:
class Animal {
void makeSound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
void callParentSound() {
super.makeSound(); // Calling parent class method
}
}
public class TestSuper {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Child class method
dog.callParentSound(); // Parent class method using super
}
}Explanation
- The Dog class calls the Animal constructor with super(name), thereby forwarding the name parameter.
- Hence, the parent class constructor gets the chance to set up the parent class portion of the object.
Output
Dog barks
Animal makes soundQuick Note: The super.method() enables subclasses to reuse and augment superclass behaviors during method overriding.
3. Use of super with Constructors
The super constructor in java calls a parent class constructor using the super keyword. This is particularly helpful when the parent class has a parameterized constructor, and you wish to call it from the child class.
Syntax
super();
super(parameters);Example Code
Here is the Java super example using constructors:
class Animal {
Animal(String name) {
System.out.println("Animal name: " + name);
}
}
class Dog extends Animal {
Dog(String name) {
super(name); // Calling parent class constructor
System.out.println("Dog name: " + name);
}
}
public class TestSuper {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
}
}Explanation
- The Animal constructor is invoked by the Dog class with super(name), passing in the name parameter.
- This allows the parent class constructor to initialize the parent class part of the object.
Output
Animal name: Buddy
Dog name: Buddy
Quick Recap: The super() triggers superclass constructor execution, ensuring correct initialization order across inheritance layers.
Understanding the key characteristics for using the super keyword effectively in Java is essential for writing robust, maintainable code that leverages inheritance, polymorphism, and constructor overloading.
Key Characteristics of the super Keyword
- Supports Inheritance in Java: The super keyword enables subclasses to access members (methods and variables) and constructors of their immediate superclass, making it fundamental to inheritance in Java.
- Constructor Chaining and Default Constructor: The super() call is used for constructor chaining, ensuring the superclass is initialized before the subclass. If not explicitly specified, Java inserts a call to the default constructor of the superclass.
- Resolves Ambiguity: When subclass members (variables or methods) hide those of the superclass, super helps avoid ambiguity and ensures the correct member is accessed.
- Facilitates Polymorphism: By allowing access to overridden methods in the superclass, super supports polymorphic behavior and method resolution.
- Cannot Access Private Members: The super keyword cannot be used to access private fields or methods of the superclass.
Best Practices for Using super Keyword
- Always Place super() as the First Statement: If you are moving up a level to a superclass constructor from a subclass, then it is super(), or super(parameters), that should be the very first statement in the constructor. This is the main thing for the correct object setup, particularly, when constructor overloading is involved.
- Use super to Enhance, Not Replace: In case you have overridden methods, employ super.methodName() to continue the base behavior rather than completely substituting it, unless a total override is the only case.
- Don't Overuse super: Emphasize with super only in situations where it is necessary to solve conflicts or gain access to superclass functionalities. If not required, using super can make the code less readable and lower in maintainability.
- Constructor Overloading Considerations: Ensure that the subclass uses the correct super() call when it decides to invoke the overloaded constructor of the superclass.
- Practical Examples: Use real-life examples in your code to demonstrate the usage of the super keyword effectively, such as solving variable shadowing or extending methods of a superclass.
Quick Points to Remember
- If the superclass does not have a default constructor and the subclass does not explicitly call a superclass constructor, a compile-time error will occur.
- The super cannot be used in static contexts.
- Use super judiciously to maintain clear and understandable inheritance hierarchies.

