Skip to main content
Clean Code 60 XP · 6 min

Abstraction Levels & Dependencies

Keep high-level policy separate from low-level detail, and ensure base classes never depend on derived classes.

Showing
Ad (728×90)

Wrong Abstraction Level

G5: Every concept in a base class should be at the same level of abstraction. High-level policy (authentication, routing) must not live in the same layer as low-level details (byte buffers, file handles). When you mix levels, neither layer is easy to read or reuse.

Base Classes Must Not Know Their Derivatives

G6: A base class that inspects type(self) or uses instanceof on its own children is not a base class anymore — it is a god object pretending to be a hierarchy. Knowledge flows downward: base → derived. The moment it flows upward, the abstraction is broken. Refactor: define an abstract method in the base class and let each derived class implement it.

Code Challenge

Refactor the Shape base class to remove the type-switch.

Key takeaway

Base classes define contracts. Derived classes own the implementation. Never reverse that flow.

Done with this lesson?

Mark it complete to earn XP and track your progress.