Skip to main content

Sign in to CleanKata

Track your progress, earn XP, and unlock every lesson.

By signing in you agree to our Terms of Use and Privacy Policy.

Clean Code60 XP6 min

Abstraction Levels & Dependencies

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

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.

🔧 Some exercises may still have errors. If something seems wrong, use the Feedback button (bottom-right of the page) to report it — it helps us fix it fast.

Hint: G6: The base class must not mention derived class names. Move the logic into each subclass.

✗ Your version