Software design and clean code

Stage 1 · Lesson 4 of 8

Inheritance, used sparingly

Object-oriented programming properly

lets a subclass take on the fields and methods of a parent class and add to or override them. It models an is-a relationship: a SavingsAccount is an Account. Used for a true is-a, one or two levels deep, it keeps related types consistent and lets a subclass be used wherever the parent is expected.

The problems start when inheritance is used just to reuse code. A subclass is tied to every detail of its parent, so a change in the parent can quietly break it. This is often called the fragile base class problem. Deep hierarchies also make it hard to know where a method really comes from.

Common trapStack extends ArrayList style reuse, which leaks every list method to callers and can never be undone without breaking them.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

  • Free exercises in your language with optional human mentoring on design, not just on passing tests

Keep handy

  • Oracle's own tutorials on classes, interfaces, records and sealed classes in current Java

Also worth your time

Mark this stage as not done?