Code Smell 290 – Refused Bequest
![](https://hackernoon.imgix.net/images/RIiBoPtpMiRsMKX3dnzl5gb1Urj1-nx02n6p.jpeg)
When you ignore your inheritance, you will have trouble with your parents
TL;DR: Subclasses should honor ALL their parentβs contract.
Problems π
Solutions π
- Favor composition over inheritance
- Don’t subclassify for code reuse
- Rethink hierarchy
- Extract shared logic
- Use interfaces
- Remove dead code
Refactorings βοΈ
Refactoring 007 – Extract Class
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 π
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.
Level π
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
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 π©ββ€οΈβπβπ¨
Code Smell 11 – Subclassification for Code Reuse
Code Smell 58 – Yo-yo Problem
Code Smell 43 – Concrete Classes Subclassified
More Information π
Refactoring Guru
Code Smells
Disclaimer π
Code Smells are my opinion.
Credits π
Photo by Hanson Lu on Unsplash
Favor object composition over class inheritance.
Erich Gamma
Software Engineering Great Quotes
This article is part of the CodeSmell Series.
How to Find the Stinky Parts of your Code