Home Knowledge Base Parallel Inheritance Hierarchies

Parallel Inheritance Hierarchies is a code smell where two separate class hierarchies mirror each other in lockstep — every time a new subclass is added to Hierarchy A, a corresponding subclass must be created in Hierarchy B, creating a maintenance dependency between the two trees that doubles the work of every extension and introduces a systematic risk that the hierarchies fall out of sync over time.

What Is Parallel Inheritance Hierarchies?

The smell manifests as two class trees that grow together:

Why Parallel Inheritance Hierarchies Matter

Refactoring: Merge Hierarchies

Move Method into Hierarchy: If Hierarchy B's classes only serve to operate on Hierarchy A's corresponding class, move the methods into Hierarchy A's classes directly. Circle gains a render() method; CircleRenderer is eliminated.

Visitor Pattern: When rendering (or any processing) logic must be separated from the shape hierarchy (e.g., for dependency reasons), the Visitor pattern provides a cleaner alternative to parallel hierarchies — a single ShapeVisitor interface with visit(Circle), visit(Rectangle) methods. Adding a new shape requires one class addition plus updating the visitor interface, with compile-time enforcement that all visitors handle the new shape.

Generics/Templates: For structural pairings like Entity/DAO, generics can eliminate the parallel hierarchy entirely: GenericDAO replaces UserDAO, OrderDAO, ProductDAO with one parameterized class.

When Parallel Hierarchies Are Acceptable

Some frameworks mandate parallel hierarchies (particularly DAO/Entity, ViewModel/Model patterns in some MVC frameworks). When dictated by architectural constraints: document the pairing rule explicitly and enforce it through code generation or convention checking rather than relying on developers to remember.

Tools

Parallel Inheritance Hierarchies is coupling the trees — the structural smell that locks two class hierarchies into a lockstep dependency relationship, doubling the work of every extension, introducing systematic synchronization risk, and dividing the logic for each concept across two separate locations that must always be updated in tandem.

parallel inheritance hierarchiescode ai

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.