01 / 21
super sub

Inheritance with
super

Learn how to extend classes and use the super keyword to call superclass constructors and methods in Java.

Java

What is Inheritance?

Inheritance is a core principle of object-oriented programming where a new class (subclass) is created from an existing class (superclass). The subclass inherits all accessible fields and methods from the superclass, then can add or modify its own.

The extends Keyword

In Java, you create a subclass by using the extends keyword. This establishes an is-a relationship: if Dog extends Animal, then every Dog is an Animal.

public class Dog extends Animal { // Dog inherits all fields and methods from Animal // Dog can add new fields and methods // Dog can override inherited methods }

Visualizing an Inheritance Hierarchy

Animal name, eat(), sleep() Dog bark(), fetch() Cat purr(), climb() Bird fly(), sing() extends extends extends extends GuideDog

The super Keyword

The super keyword in Java is a reference to the immediate parent class. It lets the subclass explicitly communicate with the superclass in two critical ways:

super(...)
Calls the superclass constructor to initialize inherited fields. Must be the first statement in the subclass constructor.
super.method()
Calls a superclass method, even if the subclass has overridden it. Useful to extend behavior rather than replace it.

Calling Superclass Constructors

When you create a subclass object, the superclass constructor must run first. Java enforces this because the superclass portion of the object must be fully initialized before the subclass adds its own state.

public class Animal { private String name; public Animal(String name) { this.name = name; } } public class Dog extends Animal { private String breed; public Dog(String name, String breed) { super(name); // MUST be first line this.breed = breed; } }

Calling Overridden Methods

When a subclass overrides a method, it replaces the superclass version. But sometimes you want to add to the superclass behavior, not replace it. Use super.methodName() to call the original.

public class Animal { public String toString() { return "Animal: " + name; } } public class Dog extends Animal { @Override public String toString() { return super.toString() + ", Breed: " + breed; } }

super() vs super.method()

super(...) super.method()
Purpose Call parent constructor Call parent method
Where used Inside subclass constructor only Inside any subclass method
Position Must be first statement Anywhere in the method
Arguments Matches parent constructor params Matches parent method params
If omitted Java inserts super() automatically Overridden version runs instead

How to Apply Inheritance with super

1
Identify the "is-a" relationship. Ask: is the new class a specialized version of the existing class?
2
Write the subclass declaration. Use extends to connect child to parent.
3
Write the subclass constructor. Call super(...) as the first line, passing required arguments.
4
Override methods as needed. Use super.method() to reuse parent logic when you want to extend, not replace.
5
Add subclass-specific fields and methods. These belong only to the subclass.

Example 1: Vehicle Hierarchy

public class Vehicle { private String make; private int year; public Vehicle(String make, int year) { this.make = make; this.year = year; } public String getInfo() { return year + " " + make; } }
public class Truck extends Vehicle { private double towCapacity; public Truck(String make, int year, double tow) { super(make, year); // initialize Vehicle part this.towCapacity = tow; } @Override public String getInfo() { return super.getInfo() + ", Tow: " + towCapacity + " tons"; } }

Tracing the Constructor Chain

Truck t = new Truck("Ford", 2024, 5.5); System.out.println(t.getInfo());
Step 1 new Truck("Ford", 2024, 5.5) Step 2: super(make, year) Vehicle("Ford", 2024) runs Step 3 this.towCapacity = 5.5 Output of t.getInfo() "2024 Ford, Tow: 5.5 tons" super.getInfo() → "2024 Ford" + Truck addition → ", Tow: 5.5 tons"

Example 2: Employee System

public class Employee { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public double pay() { return salary; } } public class Manager extends Employee { private double bonus; public Manager(String name, double salary, double bonus) { super(name, salary); this.bonus = bonus; } @Override public double pay() { return super.pay() + bonus; } }

Tracing Manager.pay()

Manager m = new Manager("Alice", 80000, 15000); System.out.println(m.pay()); // What prints?
1
m.pay() is called. Manager overrides pay(), so Manager's version runs.
2
super.pay() calls Employee's pay(), which returns 80000.0.
3
Manager adds bonus: 80000.0 + 15000.0 = 95000.0.

Example 3: Multi-Level Inheritance

public class Shape { private String color; public Shape(String color) { this.color = color; } public String describe() { return color + " shape"; } } public class Rectangle extends Shape { private double w, h; public Rectangle(String c, double w, double h) { super(c); this.w = w; this.h = h; } @Override public String describe() { return super.describe() + " (" + w + "x" + h + ")"; } } public class Square extends Rectangle { public Square(String c, double side) { super(c, side, side); // passes side for both w and h } }

Common Mistakes

✕ Forgetting super()
If the superclass has no no-arg constructor, you must explicitly call super(...) with the right arguments. Otherwise: compile error.
✕ super() Not First
Placing super(...) anywhere except the very first line of the constructor causes a compile error. No code can come before it.
✕ Accessing Private Fields
Subclasses cannot access private fields directly. Use super.getMethod() or protected/public accessors instead.

Summary: Inheritance with super

Concept Syntax Key Rule
Extend a class class B extends A Single inheritance only
Call parent constructor super(args) Must be first statement
Call parent method super.method() Use to extend, not replace
Override a method @Override Same signature as parent
Default constructor call super() auto-inserted Only if parent has no-arg

Question 1 of 5

Given: class Dog extends Animal and Animal has only one constructor: Animal(String name). Which Dog constructor is valid?

Question 2 of 5

What happens if you do not write super(...) in a subclass constructor and the superclass has no no-argument constructor?

Question 3 of 5

What does m.pay() return?

Question 4 of 5

Where must the super(...) constructor call appear in a subclass constructor?

Question 5 of 5

What does new Circle("blue", 3.0).describe() return?