36

Extract Superclass


Goal

Two classes with substantial shared structure get a common parent that owns the shared bits.

Before the refactoring
class Employee   { name; id; salary; }
class Department { name; id; budget; }
After the refactoring
class Party       { name; id; }
class Employee   extends Party { salary; }
class Department extends Party { budget; }
Savings

Bug fixes and new shared behavior land in one place; the relationship between the classes is documented in code.

Note

Inheritance is inflexible — if the duplication is shallow, prefer Extract Class (composition) over Extract Superclass.