Java super Keyword
In this page:
What is the super Keyword?
super is a reference used inside a subclass to explicitly reach its immediate parent class — its members, its constructors — rather than the subclass's own overridden or shadowed versions of the same names.
Example: What is the super Keyword?
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override
void sound() {
super.sound(); // explicitly reaches the parent's version
System.out.println("Bark");
}
}
public class Main {
public static void main(String[] args) {
new Dog().sound();
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing Parent Variables
When a subclass field or method shares a name with something in the parent and effectively hides it, super.fieldName or super.methodName() lets you deliberately reach past the subclass's version to access the parent's original.
Example: Accessing Parent Variables
class Animal {
String type = "Animal";
}
class Dog extends Animal {
String type = "Dog";
void show() {
System.out.println(type);
System.out.println(super.type);
}
}
public class Main {
public static void main(String[] args) {
new Dog().show();
}
}
Login to try C/C++/Java/PHP code in the editor
Calling Parent Constructors
super(...) explicitly invokes a parent class constructor from within a subclass constructor, and Java requires this call (if present) to be the very first statement in the subclass constructor body, before any other initialization happens.
Example: Calling Parent Constructors
class Animal {
Animal() {
System.out.println("Animal created");
}
}
class Dog extends Animal {
Dog() {
super(); // must be the first statement
System.out.println("Dog created");
}
}
public class Main {
public static void main(String[] args) {
new Dog();
}
}
Login to try C/C++/Java/PHP code in the editor
Parameterized Parent Constructors
If the parent class has no no-argument constructor — only a parameterized one — the subclass constructor is required to call super(arguments) explicitly with matching arguments, since Java won't silently insert a call to a constructor that doesn't exist.
Example: Parameterized Parent Constructors
class Animal {
Animal(String name) {
System.out.println("Animal: " + name);
}
}
class Dog extends Animal {
Dog() {
super("Rex"); // required: Animal has no no-arg constructor
}
}
public class Main {
public static void main(String[] args) {
new Dog();
}
}
Login to try C/C++/Java/PHP code in the editor
Super with Nested Hierarchies
In a multilevel hierarchy, super always resolves to exactly one level up — the immediate parent — never skipping ahead to a grandparent, which keeps constructor and method delegation predictable step by step through the chain.
Example: Super with Nested Hierarchies
class A {
void show() { System.out.println("A"); }
}
class B extends A {
@Override
void show() { System.out.println("B"); }
}
class C extends B {
void callSuper() {
super.show(); // resolves to B, not A
}
}
public class Main {
public static void main(String[] args) {
new C().callSuper();
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: