The super keyword in Java is a reference variable that is used to refer to the immediate parent class object.
Whenever you create an instance of a subclass, an instance of the parent class is created implicitly, which is referred to by the super reference variable.
super Keywordsuper() can be used to call the parent class's constructor.One of the most common uses of super is to call the constructor of the parent class from the child class's constructor. This is essential when the parent class does not have a default (no-argument) constructor.
The call to super() must be the very first statement in the child class constructor.
class Vehicle {
String brand;
// Parent class constructor
public Vehicle(String brand) {
this.brand = brand;
System.out.println("Vehicle constructor called.");
}
public void displayBrand() {
System.out.println("Brand: " + brand);
}
}
class Car extends Vehicle {
String model;
// Child class constructor
public Car(String brand, String model) {
// Call the parent constructor using super()
super(brand);
this.model = model;
System.out.println("Car constructor called.");
}
public void displayModel() {
System.out.println("Model: " + model);
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of the subclass will call both constructors
Car myCar = new Car("Ford", "Mustang");
myCar.displayBrand();
myCar.displayModel();
}
}
In this example, super(brand) in the Car constructor calls the Vehicle(String brand) constructor, ensuring that the brand attribute from the parent class is properly initialized.
You can use super to call a method from the parent class, even if you have overridden it in the child class.
class Animal {
public void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
@Override
public void eat() {
super.eat(); // Calls the parent's eat() method
System.out.println("This dog eats dog food.");
}
}
// In main, you would call it like this:
// Dog myDog = new Dog();
// myDog.eat();
If a child class declares a variable with the exact same name as a variable in the parent class, the child's variable hides (or "shadows") the parent's variable.
If you are inside the child class and need to access the parent's hidden variable, you must use super.variableName.
class Parent {
int speed = 50; // Parent variable
}
class Child extends Parent {
int speed = 100; // Shadows the parent variable
public void displaySpeeds() {
System.out.println("Child speed (this.speed): " + this.speed);
System.out.println("Parent speed (super.speed): " + super.speed);
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.displaySpeeds();
}
}
super with Interfaces (Java 8+)Since Java 8, interfaces can contain default methods (methods with actual bodies). But what happens if a class implements two interfaces that both have a default method with the exact same name? This is a form of the Diamond Problem!
Java forces the class to override the method to resolve the conflict. If you still want to call one of the interface's default implementations, you use a special syntax: InterfaceName.super.methodName().
interface Engine {
default void start() { System.out.println("Engine is starting..."); }
}
interface Battery {
default void start() { System.out.println("Battery is turning on..."); }
}
class HybridCar implements Engine, Battery {
@Override
public void start() {
// Specifically call the Engine's default method
Engine.super.start();
System.out.println("Hybrid Car is ready to go!");
}
}
super refers to the immediate parent class portion of the current object.super(...) to call a parent constructor. It must be the first statement in the child constructor.super.methodName() when an override needs to extend, not replace, parent behavior.super cannot skip levels in the inheritance chain; it only targets the direct superclass.Which keyword is used to refer to the immediate parent class object?