Inheritance: Reuse and Extend Class Behavior
Introduction
Inheritance lets a child class reuse and extend a parent class’s fields and methods. It reduces duplication and models “is-a” relationships (Dog is an Animal). This chapter covers extends, super, and method overriding—foundations for Abstract Classes and Polymorphism.
Prerequisites
What Is Inheritance
The subclass (child) inherits accessible members of the superclass (parent).
public class Animal {
public void eat() {
System.out.println("Eating...");
}
}
public class Dog extends Animal {
public void bark() {
System.out.println("Woof!");
}
}Dog dog = new Dog();
dog.eat(); // inherited
dog.bark(); // own methodJava supports single inheritance for classes: one direct parent class. Interfaces add multiple type contracts later (Interfaces).
1) extends Keyword
public class Employee {
protected String name;
public Employee(String name) {
this.name = name;
}
}
public class Manager extends Employee {
private int teamSize;
public Manager(String name, int teamSize) {
super(name); // call parent constructor
this.teamSize = teamSize;
}
}2) super — Parent Access
super refers to the superclass:
public Manager(String name, int teamSize) {
super(name);
this.teamSize = teamSize;
}
@Override
public String toString() {
return super.toString() + " (team: " + teamSize + ")";
}super(...)— call parent constructor (must be first statement in constructor)super.method()— call overridden parent method
3) Method Overriding
Subclass provides a new implementation for a parent instance method with the same signature.
public class Animal {
public void speak() {
System.out.println("Animal sound");
}
}
public class Cat extends Animal {
@Override
public void speak() {
System.out.println("Meow");
}
}Rules (simplified):
- Same method name and compatible parameter list
- Return type compatible (covariant returns allowed)
- Not weaker access (public stays public)
- Instance methods only—not static methods (static hiding is different)
Use @Override so the compiler verifies you truly override (Annotations).
4) protected Visibility
protected members are visible to subclasses and same package—between private and public. Full table: Packages.
5) Design Guidelines
Use inheritance when:
- Relationship is truly “is-a” (
Squareis aRectangle—debate carefully) - Shared behavior belongs in parent
Avoid:
- Deep trees only for code reuse—prefer composition (“has-a”)
- Overriding without need—makes behavior harder to trace
6) final Classes and Methods
final class— cannot be extendedfinal method— cannot be overridden
public final class SecurityToken { }Use when extension would break invariants (e.g. String is final).
Common Beginner Mistakes
Forgetting super() in Constructor
If parent has no no-arg constructor and child constructor does not call super(...), compile error.
Accidental Hiding Instead of Overriding
Static methods are hidden, not overridden. Call depends on reference type for static methods.
Calling Overridden Method from Constructor
Parent constructor runs before child fields initialize—avoid complex overrides in constructors.
Mini Practice
Create Shape with area() returning 0, Circle and Rectangle overriding area().
What’s Next
Partial blueprints: Abstract Classes, then Object.
FAQ
Can a class extend multiple classes?
No. Use multiple interfaces for several contracts.
What is the difference between overriding and overloading?
- Overloading (Methods): same name, different parameters, compile-time
- Overriding: same signature in child, runtime dispatch
Is Object the root of all classes?
Yes. Every class extends Object directly or indirectly (Object Class).
Should every parent method be overridden?
Only when child behavior differs. Inherit otherwise.
When should I use super.method()?
When child logic extends parent behavior (add steps before/after), not replaces entirely.