Inheritance
Inheritance lets a class reuse and extend another class. The subclass gets the parent's fields and methods, then adds or replaces what it needs.
Detailed Explanation
Inheritance answers "is a". A SavingsAccount is an Account. It already has a balance and a deposit method. It adds an interest rate. Callers who only know about Account can still hold a savings account, because every savings account is an account.
Java allows one superclass. extends names it. If you write none, you extend Object. Interfaces add extra contracts through implements, which is not the same as class inheritance: you inherit code from one class and obligations from many interfaces.
What the child actually receives depends on access. public and protected members are visible in the subclass. Package-private members are visible only when both classes share a package. private members exist in the object layout but the child cannot name them. Constructors are not inherited at all. The child must call super(...).
Overrides replace a parent method with a more specific one. The signature must match, the return type may be a subtype (covariant), and @Override makes the compiler check you actually overrode something. final methods cannot be overridden. final classes cannot be extended. Those locks exist so library authors can change internals without breaking unknown subclasses.
Inheritance is the wrong tool when the relationship is "has a". A Car has an Engine. Making Car extend Engine gives you a car that is an engine, which is a lie, and it couples the two types forever. Prefer a field. That is composition, and it is usually the better default.
You reach for inheritance when you have a stable base type, a handful of variants, and code that should work uniformly on the base. Template-method designs, exception hierarchies, and GUI widgets are classic cases. You avoid it when you only want to reuse a utility method, when the parent is in a third-party library you cannot control, or when each subclass would disable half the parent API.
The cost is coupling. A change to a protected field or to the order of calls in a parent method can break every child. Deep trees are hard to read. Multiple layers of super hide the method that actually runs. Keep trees shallow, keep the parent abstract enough that it does not invent unused behaviour, and treat protected as a published API.
Code Examples
class Account {
protected int balancePaise;
Account(int balancePaise) {
this.balancePaise = balancePaise;
}
void deposit(int paise) {
balancePaise += paise;
}
int balancePaise() {
return balancePaise;
}
}
class SavingsAccount extends Account {
private final int interestBps; // 150 means 1.50%
SavingsAccount(int balancePaise, int interestBps) {
super(balancePaise); // parent has no no-arg constructor
this.interestBps = interestBps;
}
void applyInterest() {
balancePaise += balancePaise * interestBps / 10_000;
}
public static void main(String[] args) {
SavingsAccount a = new SavingsAccount(100_000, 150);
a.deposit(5_000);
a.applyInterest();
System.out.println(a.balancePaise()); // prints: 106575
}
}final class Isbn {
// final class: nobody can extend Isbn and break its parsing rules.
private final String value;
Isbn(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}Common Gotchas
Protected fields become a public API for subclasses
changing them later breaks children you do not compile. Prefer protected methods.
Inheritance is not reuse of convenience
extending `ArrayList` to add one helper couples you to every future `List` method. Wrap the list instead.
`instanceof` and pattern matching see the runtime class
a variable of type `Account` may still be a `SavingsAccount`.