“Inheritance lets you reuse code.” You will find that sentence in nearly every first textbook on object-oriented programming, and it is the reason so many codebases end up with hierarchies five levels deep that nobody dares touch. Reuse is a side effect of inheritance. It is not the purpose, and treating it as the purpose causes real damage.

What inheritance actually claims

When class Savings extends Account, you are making a promise: anywhere the program expects an Account, a Savings can be used instead, and nothing will go wrong. That substitutability is the Liskov Substitution Principle, the L in SOLID. The relationship is “is-a”, and the test is behavioural, not linguistic. It is not “does a savings account sound like a kind of account”, it is “will every piece of code written against Account still behave correctly when handed a Savings“.

The classic failure is a Square that extends Rectangle. Geometry says a square is a rectangle. But a rectangle lets you set width and height independently, and code relying on that (set width to 4, height to 5, expect area 20) breaks for a square, whose sides must stay equal. Is-a in the real world did not translate into is-a in behaviour.

A real one from the Java standard library: Stack extends Vector. A stack should allow only push and pop at one end. Because it inherits from Vector, it also exposes insertion at any index. Anyone can break the stack discipline, and the library can never fix that without breaking compatibility. The designers inherited to save effort, and the cost has lasted decades. The documentation itself now recommends using the Deque interface instead.

The fragile base class

Inheritance couples a child to the parent’s implementation, not merely its interface. Suppose a base class has addAll, which internally calls add for each item. A subclass overrides add to count how many items were added, and also overrides addAll to add the size of the collection to the count. Now call addAll with three items: the subclass adds 3, then calls the parent’s addAll, which calls the overridden add three times, and the count reads 6. This example is well known from Joshua Bloch’s Effective Java, and it makes the point sharply. The subclass author needed to know that addAll calls add, a private detail that the parent team may change in any release.

That is the fragile base class problem: changes to a parent, which look safe from the parent’s side, break children it has never seen.

Has-a: hold it, do not become it

Composition means your class keeps another object as a field and calls its public methods. Take a courier-tracking service for a firm in Visakhapatnam (invented). It must send notifications by SMS, email or WhatsApp, and also log every send and retry on failure.

The inheritance route: Notifier, then SmsNotifier, EmailNotifier, then RetryingSmsNotifier, LoggingRetryingEmailNotifier. Each new combination of behaviours demands a new class. Three channels times retry on or off times logging on or off is twelve classes, and the count doubles with every optional behaviour.

The composition route: define a small Channel interface with one method, send. Write three implementations. Then write a RetryingChannel that holds any other Channel and retries around its call, and a LoggingChannel that does the same for logging. Assemble at start-up: logging wrapped around retry wrapped around SMS. Three channels and two wrappers, five small classes, any combination. This is the decorator pattern, and it is composition doing the work inheritance cannot.

Other gains are practical. In a test you pass a fake Channel. At runtime you can swap the implementation without recompiling the callers. And your class exposes only the methods you choose, not everything the parent happens to have.

So is inheritance bad?

No, and “never use inheritance” is as lazy as “always use it”. It fits when all of the following hold: the relationship is a genuine, stable is-a; the parent was designed for extension, with documented hook points; the hierarchy is shallow; and you control both sides or the parent is explicitly meant to be subclassed (a framework’s abstract base, for instance). Many frameworks lean on it by design, such as Android’s Activity or Django’s class-based views. Even there, shallow wins.

Interfaces occupy a useful middle path. Implementing an interface gives substitutability (the good part of inheritance) with no inherited state or implementation (the fragile part). Newer languages push this direction: Go has no inheritance at all, only composition through embedding and interfaces, and Rust uses traits.

A quick test before you type “extends”

  • Could I honestly pass this child anywhere the parent is accepted, with every parent promise intact?
  • Am I inheriting to get a method, or to be a kind of thing? If it is just a method, hold the object instead.
  • Will I need to override something and call the parent’s version in the middle? That is a sign of tight coupling.
  • If the parent author changes internals next year, will my class notice?

When in doubt, compose first. Turning a composed design into an inherited one later is easy. Untangling a deep hierarchy is the kind of project that eats a quarter.

Figures and names in the examples are illustrative.

Leave a Reply

Trending