← Back to Core Java Course | Chapter 8: Inheritance | Lesson 6 of 7

Java super Keyword

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?

java
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();
	}
}

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

java
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();
	}
}

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

java
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();
	}
}

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

java
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();
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.