Ignoring your inheritance leads to conflicts with your origins.
TL;DR: Subclasses should honor ALL their parent’s contract.
Problems 😔
- Wrong abstraction
- Subclassification for code reuse
- Misleading hierarchy
- Unused or overridden methods
- Unnecessary complexity
- Liskov Principle Violation
- Maintenanabiilty
- YoYo Hierachies
- Concrete classes subclassified
Solutions 😃
- Favor composition over inheritance
- Don't subclassify for code reuse
- Rethink hierarchy
- Extract shared logic
- Use interfaces
- Remove dead code
Refactorings ⚙️
Context 💬
When you create a subclass, it should use or extend the behavior of its parent.
If it ignores or overrides most of it, you probably force inheritance where it doesn’t belong to reuse code.
This makes the code misleading and hard to maintain.
Sample Code 📖
Wrong 🚫
class House {
constructor(address) {
this.address = address;
}
address() {
return this.address;
}
openDoor() {
console.log("Door opened at " + this.address);
}
}
class Motorhome extends House {
constructor() {
super(null);
}
address() {
return null;
// This implementation is the same as the parent's
// and is also a refused bequest
}
openDoor() {
console.log("Motorhome door opened.");
}
}
Right 👉
class House {
constructor(address) {
this.address = address;
}
address() {
return this.address;
}
openDoor() {
console.log("Door opened at " + this.address);
}
}
class Motorhome {
// does not inherit from House
constructor(gps) {
this.gps = gps;
}
openDoor() {
console.log("Motorhome door opened at " + this.gps.getLocation());
}
}
Detection 🔍
[X] Manual
Look for subclasses that override or ignore most of their parent’s behavior.
You should reconsider the inheritance if a subclass sets parent properties to null or reimplements core methods.
Tags 🏷️
- Inheritance
Level 🔋
[X] Intermediate
Why the Bijection Is Important 🗺️
Your software should reflect real-world relationships.
When you force a subclass that doesn’t logically extend its parent in the Bijection, you mislead developers and introduce maintenance problems.
AI Generation 🤖
AI can generate this smell when it defaults to inheritance for reuse instead of composition.
This happens when AI follows generic templates without understanding the context.
AI Detection 🥃
AI can detect this smell by analyzing class structures and inheritance trees. However, it struggles with subtle cases where inheritance initially seems valid but breaks expectations.
Try Them! 🛠
Remember: AI Assistants make lots of mistakes
Without Proper Instructions | With Specific Instructions |
---|---|
ChatGPT | ChatGPT |
Claude | Claude |
Perplexity | Perplexity |
Copilot | Copilot |
Gemini | Gemini |
DeepSeek | DeepSeek |
Meta AI | Meta AI |
Conclusion ✔️
When you design a class hierarchy, you need to make sure that subclasses logically inherit from their parent.
If a subclass refuses some of the behavior, you should rethink your design.
Relations 👩❤️💋👨
![mcsee](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366059%2F1d8942bc-727d-4d1e-bc50-30ff073a34fc.jpeg)
Code Smell 11 - Subclassification for Code Reuse
Maxi Contieri ・ Oct 30 '20
![mcsee](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366059%2F1d8942bc-727d-4d1e-bc50-30ff073a34fc.jpeg)
Code Smell 43 - Concrete Classes Subclassified
Maxi Contieri ・ Dec 5 '20
More Information 📕
Disclaimer 📘
Code Smells are my opinion.
Credits 🙏
Photo by Hanson Lu on Unsplash
Favor object composition over class inheritance.
Erich Gamma
![mcsee](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366059%2F1d8942bc-727d-4d1e-bc50-30ff073a34fc.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
![mcsee](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366059%2F1d8942bc-727d-4d1e-bc50-30ff073a34fc.jpeg)
Top comments (0)