I'll Be Backend
Principles

Inheritance: Pull Up Duplicated Behavior

Inheritance lets a subclass get the fields and methods of its superclass, so code that the subclasses share is written once. These lessons take a courier's two shipment classes that each repeat the tracking code, the weight and the label, pull the fields, the constructor body and the label up into Shipment, and then add a fragile flag in one place.

Pick your language

When to use it

Use it when

Two or more subclasses of one superclass repeat the same fields or the same method body.

Skip it when

Two classes look alike today but aren't the same kind of thing. Extract Class for the shared part and delegate to it.

Spot it by

The same method copied into sibling subclasses, and the same fields and constructor lines in each.