Inheritance with
super
Learn how to extend classes and use the super keyword to call superclass constructors and methods in 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.
Visualizing an Inheritance Hierarchy
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:
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.
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.
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
Example 1: Vehicle Hierarchy
Tracing the Constructor Chain
Example 2: Employee System
Tracing Manager.pay()
Example 3: Multi-Level Inheritance
Common Mistakes
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
class Employee has method pay() returning salary.
class Manager extends Employee overrides pay() as:
return super.pay() + bonus;
Manager m = new Manager("Jo", 60000, 10000);
What does m.pay() return?
Question 4 of 5
Where must the super(...) constructor call appear in a subclass constructor?
Question 5 of 5
class Shape { String color; Shape(String c) { color = c; } String describe() { return color + " shape"; } }
class Circle extends Shape { double r; Circle(String c, double r) { super(c); this.r = r; } String describe() { return super.describe() + " r=" + r; } }
What does new Circle("blue", 3.0).describe() return?