class Animal {
void eat() {
System.out.println("Animal is eating");
}
}class Dog extends Animal {
void show() {
super.eat();
}
}The super Keyword in Java: Parent Class Access and Method Calls
Direct Answer & Core Concept
When you start learning Java inheritance, you will often work with a parent class and a child class. Sometimes the child class has its own method or variable, but you still need to use something from the parent class.
This is where the super keyword in Java becomes useful.
The super keyword allows a subclass to refer to its immediate parent class. You can use it to call a parent constructor, access a parent variable, or run a parent method.
For beginners, the easiest way to remember it is:
supermeans “go to my immediate parent class.”
For example, if Dog extends Animal, then super inside Dog refers to Animal.
Here, super.eat() tells Java to use the eat() method from the parent class.
Detailed Explanation & Mechanics
What Is the super Keyword in Java?
The super keyword in Java is used inside a subclass to refer to its immediate superclass.
It is mainly useful when the child class needs to access something specifically defined in its parent class.
Java provides three common uses of super:
super()– calls the parent class constructor.super.methodName()– calls a parent class method.super.variableName– accesses a parent class variable.
These three forms look similar, but they solve different problems.
Simple Example
class Vehicle {
String type = "Vehicle";
void start() {
System.out.println("Vehicle is starting");
}
}class Car extends Vehicle {
void display() {
System.out.println(super.type);
super.start();
}
}In this example:
super.typeaccesses the parent variable.super.start()calls the parent method.
Why Do We Need super in Java?
You may wonder why Java needs the super keyword when a child class already inherits members from its parent.
In many simple situations, you do not need to write super.
However, problems can appear when the child class has a member with the same name as the parent, or when the child overrides a parent method.
For example:
class Employee {
String name = "Rahul";
}class Manager extends Employee {
String name = "Amit";
void showNames() {
System.out.println(name);
System.out.println(super.name);
}
}Output:
AmitRahulThe first name refers to the child class variable.
The super.name expression specifically refers to the parent class variable.
So, super is useful when you want to remove confusion between parent and child members.
Uses of super Keyword in Java
The super keyword is mainly used in three ways.
| Syntax | Purpose |
|---|---|
super() | Calls the parent class constructor |
super.method() | Calls the parent class method |
super.variable | Accesses the parent class variable |
Let's understand each one with simple examples.
1. Calling the Parent Class Constructor Using super()
A constructor is used when an object is created.
When a child object is created, the parent part of that object must also be initialized. Java handles this parent initialization through constructor calls.
You can explicitly call a parent constructor using:
super();or:
super(value);The super() statement must be the first statement inside a constructor.
Example of super() in Java
class Animal {
Animal(String type) {
System.out.println("Animal type: " + type);
}
}class Dog extends Animal {
Dog() {
super("Dog");
System.out.println("Dog object created");
}
}public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
}
}Output
Animal type: DogDog object createdHow does this work?
When this line runs:
Dog dog = new Dog();Java starts executing the Dog constructor.
The first statement is:
super("Dog");That calls the constructor of Animal.
After the parent constructor finishes, the remaining statements in the Dog constructor execute.
So the order is:
Dog object created↓Dog constructor starts↓super("Dog")↓Animal constructor↓Dog constructor continuesWhy Is super() Important in Constructor Chaining?
Constructor chaining means one constructor calls another constructor as part of object initialization.
With inheritance, super() can connect the child constructor to a constructor in the parent class.
For example:
class Person {
Person(String name) {
System.out.println("Name: " + name);
}
}class Student extends Person {
Student(String name) {
super(name);
System.out.println("Student created");
}
}Here:
super(name);passes the value from the child constructor to the parent constructor.
This is useful when the parent class is responsible for initializing common information.
The reference material also highlights constructor chaining as one of the important situations where super() is used.
What Happens If You Do Not Write super()?
If you do not explicitly write a super() call in a subclass constructor, Java tries to insert a call to the parent class's no-argument constructor automatically.
For example:
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}class Dog extends Animal {
Dog() {
System.out.println("Dog constructor");
}
}Conceptually, Java treats the child constructor as if it contained:
Dog() {
super();
System.out.println("Dog constructor");
}This automatic behavior works when the parent class has an accessible no-argument constructor.
If the parent class only has a parameterized constructor, the child constructor must explicitly call an appropriate parent constructor.
Example:
class Animal {
Animal(String name) {
System.out.println(name);
}
}class Dog extends Animal {
Dog() {
super("Rocky");
}
}Without the explicit super("Rocky"), the code would not compile because Java would try to call Animal() and that constructor does not exist.
2. Calling the Parent Class Method Using super
Another important use of super is calling a method from the parent class.
This becomes especially useful when the child class overrides a method.
Suppose the parent has a method called sound():
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}The child class can override it:
class Dog extends Animal {
@Override void sound() {
System.out.println("Dog barks");
}
}Normally, calling:
dog.sound();runs the child version.
But what if the child also wants to execute the original parent behavior?
Use:
super.sound();Java super Keyword with Method Overriding
Here is a complete example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}class Dog extends Animal {
@Override void sound() {
super.sound();
System.out.println("Dog barks");
}
}public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}Output
Animal makes a soundDog barksThe statement:
super.sound();runs the parent version first.
Then:
System.out.println("Dog barks");adds the child-specific behavior.
This is one of the most practical uses of the Java super keyword.
Why Use super with Method Overriding?
Using super.methodName() is useful when you do not want to completely replace the parent behavior.
For example, imagine a parent class performs common logging:
class Account {
void openAccount() {
System.out.println("Account opened");
}
}A child class can add its own behavior:
class SavingsAccount extends Account {
@Override void openAccount() {
super.openAccount();
System.out.println("Savings account features enabled");
}
}The child gets the original parent behavior and then adds something extra.
This approach can prevent unnecessary duplication of code.
The source material similarly identifies reusing parent logic inside an overridden method as a common reason to use super.
3. Accessing a Parent Class Variable Using super
A child class can have a variable with the same name as a variable in its parent.
This is called field hiding or variable shadowing.
In that situation, super.variableName lets you access the parent version.
Example
class Animal {
String name = "Animal";
}class Dog extends Animal {
String name = "Dog";
void display() {
System.out.println("Child name: " + name);
System.out.println("Parent name: " + super.name);
}
}public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.display();
}
}Output
Child name: DogParent name: AnimalHere:
namerefers to the variable declared in Dog.
But:
super.namerefers to the variable declared in Animal.
Difference Between name and super.name
This is an important point for beginners.
Suppose:
class Parent {
String value = "Parent";
}class Child extends Parent {
String value = "Child";
void show() {
System.out.println(value);
System.out.println(super.value);
}
}The result is:
ChildParentThink of it like this:
value↓Current class → Childsuper.value↓Immediate parent → ParentThe super keyword removes the ambiguity.
super with Method Overriding vs Variable Hiding
Methods and variables behave differently in Java, so it is useful to keep the two cases separate.
Parent method
super.display();Calls the parent implementation of the method.
Parent variable
super.name;Reads the parent class field.
For methods, super is particularly useful when the child overrides a method.
For fields, super is useful when the child declares a field with the same name.
super in Multi-Level Inheritance
Java supports inheritance across multiple levels.
For example:
Animal↓Dog↓PuppyHere:
Animalis the grandparent class.Dogis the parent ofPuppy.Puppyis the child class.
One important rule is that super refers to the immediate parent, not directly to any class farther up the hierarchy.
For example:
class Animal {
void eat() {
System.out.println("Animal eats");
}
}class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}class Puppy extends Dog {
void show() {
super.bark();
}
}Here:
super.bark();refers to Dog, because Dog is the immediate parent of Puppy.
It does not directly mean Animal.
The source material also notes that in multi-level inheritance, super refers to the immediate parent class.
Can super Access Private Members?
No.
A child class cannot directly access private fields or private methods of its parent class using super.
For example:
class Parent {
private int number = 10;
}class Child extends Parent {
void show() {
// System.out.println(super.number); // Error
}
}The variable number belongs only to the private scope of Parent.
If the parent wants to expose that value, it can provide a method:
class Parent {
private int number = 10;
int getNumber() {
return number;
}
}Then the child can call:
class Child extends Parent {
void show() {
System.out.println(super.getNumber());
}
}So, super does not bypass Java's access control rules. The reference source also explicitly notes that private parent members cannot be accessed directly through super.
Can We Use super in a Static Method?
No.
The super keyword is associated with the current object and its parent class. A static method belongs to the class rather than a particular object.
Therefore, Java does not allow super inside a static method.
For example:
class Parent {
void show() {
System.out.println("Parent");
}
}class Child extends Parent {
static void test() {
// super.show(); // Compile-time error
}
}If you need to use super, place the code in an instance method or constructor.
The provided reference also identifies static methods as a situation where super cannot be used.
Can We Use super() and this() Together?
No.
Inside one constructor, you cannot directly use both:
super();and:
this();Both constructor calls must appear as the first statement, so Java does not allow them together in the same constructor.
For example, this is invalid:
class Child extends Parent {
Child() {
this(10);
super();
}
}Instead, you normally choose one constructor call.
this()
Calls another constructor in the same class.
super()
Calls a constructor in the immediate parent class.
The reference source also identifies this as an important rule for Java constructors.
super() vs this()
Beginners often confuse these two keywords.
| Keyword | Used for | Example |
|---|---|---|
this() | Calls another constructor in the same class | this(10); |
super() | Calls a constructor in the parent class | super(10); |
this.name | Refers to current class field | this.name |
super.name | Refers to parent class field | super.name |
this.show() | Calls current class method | this.show() |
super.show() | Calls parent class method | super.show() |
A simple memory trick:
this → current classsuper → parent classA Complete Example of super in Java
Let's combine the main concepts into one example.
class Employee {
String name;
Employee(String name) {
this.name = name;
} void work() {
System.out.println("Employee is working");
}
}class Developer extends Employee {
String name;
Developer(String name) {
super(name);
this.name = name;
} @Override void work() {
super.work();
System.out.println("Developer is writing code");
} void showNames() {
System.out.println("Developer name: " + name);
System.out.println("Employee name: " + super.name);
}
}public class Main {
public static void main(String[] args) {
Developer developer = new Developer("Amit");
developer.work();
developer.showNames();
}
}Output
Employee is workingDeveloper is writing codeDeveloper name: AmitEmployee name: AmitThis example demonstrates:
super(name);calling the parent constructor.
super.work();calling the parent method.
super.name;accessing the parent field.
How super Works During Object Creation
When you create a child object, Java initializes the parent part before completing the child constructor.
Consider:
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}class Child extends Parent {
Child() {
super();
System.out.println("Child constructor");
}
}Creating the object:
Child obj = new Child();produces:
Parent constructorChild constructorThe parent constructor runs first.
You can visualize the process like this:
Create Child object
│
▼
Call Child constructor
│
▼
super()
│
▼
Parent constructor
│
▼
Return to Child constructor
│
▼
Child initializationThis is why parent initialization happens before child-specific constructor code.
When Should You Use super in Java?
You do not need to use super everywhere.
Use it when the code actually needs to refer to the parent class.
Use super() when:
The child constructor needs to call a specific parent constructor.
The parent class requires values during initialization.
You want clear constructor chaining.
Use super.method() when:
A child overrides a parent method.
You want to keep the original parent behavior.
You want to add child-specific behavior after parent logic.
Use super.variable when:
Parent and child fields have the same name.
You need the value stored in the parent field.
You want to make the source of a field explicit.
Common Mistakes With the super Keyword
1. Putting super() after another statement
This is incorrect:
Child() {
System.out.println("Hello");
super();
}super() must be the first statement in the constructor.
Correct:
Child() {
super();
System.out.println("Hello");
}2. Trying to use super in a static method
This is not allowed:
static void test() {
// super.show();
}super requires an instance context.
3. Using super to access private fields
This does not work:
class Parent {
private int age = 30;
}class Child extends Parent {
void show() {
// System.out.println(super.age);
}
}Use an accessible method such as a getter instead.
4. Thinking super means any ancestor
super refers to the immediate parent class.
If the hierarchy is:
A↓B↓CInside C:
superrefers to B, not directly to A.
5. Confusing super with this
Remember:
this = current classsuper = immediate parent classThis simple distinction solves many beginner mistakes.
Advantages of Using super in Java
The super keyword makes inheritance code easier to manage in several situations.
1. Reuse parent functionality
You can call an existing parent method instead of writing the same code again.
2. Avoid naming confusion
super.name clearly identifies the parent field.
3. Support constructor chaining
super() connects a child constructor with a parent constructor.
4. Extend overridden methods
A child can run parent logic and then add its own behavior.
5. Make inheritance easier to understand
When reading code, super clearly tells you that the operation belongs to the parent class.
Quick Difference: super() vs super.method() vs super.variable
These three forms are worth memorizing.
super();Calls the parent constructor.
super.display();Calls the parent method.
super.name;Accesses the parent field.
A quick visual:
super
│
┌───────────┼───────────┐
│ │ │
▼ ▼ ▼
super() super.method() super.variable
│ │ │
▼ ▼ ▼
Constructor Method FieldReal-World Example of super
Imagine an application with different types of employees.
The parent class contains common employee behavior:
class Employee {
void login() {
System.out.println("Employee logged in");
}
}A developer class extends it:
class Developer extends Employee {
@Override void login() {
super.login();
System.out.println("Developer dashboard opened");
}
}When:
Developer developer = new Developer();
developer.login();runs, the output is:
Employee logged inDeveloper dashboard openedThe parent handles the common login process.
The child then adds its own behavior.
This pattern is useful in applications where several child classes share common functionality but also need their own specific logic.
Important Rules of super Keyword in Java
Keep these rules in mind:
superrefers to the immediate parent class.super()calls the parent constructor.super()must be the first statement in a constructor.super.method()calls the parent implementation of an accessible instance method.super.variableaccesses an accessible parent field.Private parent members cannot be accessed directly using
super.supercannot be used in a static method.A constructor cannot directly use both
this()andsuper().If no constructor call is written, Java attempts to insert
super()automatically when the parent has an accessible no-argument constructor.In multi-level inheritance,
superalways refers to the immediate parent.
Java super Keyword: Quick Revision
If you are preparing for an exam, interview, or Java coding test, remember this:
super
│
├── super() → Parent constructor
│
├── super.method() → Parent method
│
└── super.variable → Parent variableExample:
class Parent {
String name = "Parent";
Parent() {
System.out.println("Parent constructor");
} void show() {
System.out.println("Parent method");
}
}class Child extends Parent {
String name = "Child";
Child() {
super();
} void display() {
System.out.println(super.name);
super.show();
}
}This single example demonstrates all three major forms of super.
Runnable Code Example: The super Keyword in Java
Java 17+class ParentService {
void start() {
System.out.println("Parent service starting");
}
}class ChildService extends ParentService {
@Override void start() {
super.start();
// Run parent initialization first
System.out.println("Child service specific hooks configured");
}
}public class SuperDemo {
public static void main(String[] args) {
new ChildService().start();
}
}