super Keyword — Interactive LearningsuperThe super keyword is the bridge between a parent class and its subclass. It ensures subclasses are built upon a stable, initialized foundation and allows explicit reuse of parent constructors, methods, and fields.
When an object is created in an inheritance chain, Java initializes the parent part first (via the parent constructor) and then the subclass. super() makes this explicit and controlled. Using super improves clarity when members are shadowed or methods overridden — the intention becomes clear.
The super keyword lets a subclass access its parent’s variable when it’s hidden by a field of the same name.
Look at Child.java, which illustrates how super accesses the parent's field when the subclass has a field with the same name.
class Parent {
int value = 100;
}
public class Child extends Parent {
int value = 200;
void show() {
System.out.println("Child value: " + value);
System.out.println("Parent value: " + super.value);
}
public static void main(String[] args) {
Child obj = new Child();
obj.show();
}
}When a subclass overrides a method of a superclass, the parent’s version is hidden; using super.methodName() allows calling the parent version explicitly.
Look at Child.java, which demonstrates calling a parent's method from the subclass using super.methodName().
class Parent {
void display() { System.out.println("Display from Parent"); }
}
public class Child extends Parent {
void display() { System.out.println("Display from Child"); }
void show() { super.display(); }
public static void main(String[] args) {
Child obj = new Child();
obj.show();
}
}The super() call inside a subclass constructor explicitly invokes the matching parent class constructor to ensure proper and controlled initialization.
Look at Child.java, which shows super() used to call a parent constructor.
class Parent {
// default constructor of the Parent class
Parent() { System.out.println("Parent (NBKRIST) constructor");
}
}
public class Child extends Parent {
// default constructor of the Child class
Child() {
super(); // calling parent constructor
System.out.println("Child (Department of CSE) constructor");
}
public static void main(String[] args) {
new Child();
}
}The super(parameterlist) call in a sub class constructor invokes the matching parent class constructor with the specified arguments.
Look at Child.java, which shows how super is used to call a parameterized parent constructor to build base foundation first.
class Parent {
String collegeName;
int establishedYear;
Parent(String name, int year) {
collegeName = name;
establishedYear = year;
System.out.println("Parent Constructor: " + collegeName + " - Established " + establishedYear);
}
}
public class Child extends Parent {
String department;
Child(String name, int year, String dept) {
super(name, year); // calling parent constructor with parameters
department = dept;
System.out.println("Child Constructor: Department - " + department);
}
void displayDetails() {
System.out.println("College: " + collegeName);
System.out.println("Year: " + establishedYear);
System.out.println("Department: " + department);
}
public static void main(String[] args) {
Child c = new Child("NBKRIST", 1979, "Computer Science");
c.displayDetails();
}
}
Constructor chaining across inheritance ensures that when a subclass object is created, constructors from the parent to the child execute in order, forming a complete and consistent object initialization chain.
Look at C.java, which demonstrates how super() ensures constructors run in correct order across inheritance levels.
class A {
A() { System.out.println("A constructor"); }
}
class B extends A {
B() { super(); System.out.println("B constructor"); }
}
public class C extends B {
C() { super(); System.out.println("C constructor"); }
public static void main(String[] args) {
new C();
}
}super keyword in Java?
super()?
super(args).super help when a method is overridden in the subclass?
super.methodName()), which is useful to extend — not fully replace — parent behavior.super() explicitly?
super() call; if the parent has no such constructor, a compilation error occurs — you must add super(args).super improve code readability?
super clarifies intent and preserves readable inheritance flow.super() to call parent constructors (must be first in constructor).super.field to access hidden fields; super.method() to call overridden methods.super to make inheritance intent explicit and code readable.