Refused Bequest

When a class unnecessarily inherits code from its parents.

Sign of Smell

The subclass does not use most of the functionalities of its parents or overrides them with empty or meaningless implementations. In fact, the SuperClass and SubClass are quite different from each other. Rather than tolerating inheritance, you tend to write code that refuses the "bequest".

Reason of Smell

  • Violation of Liskov Substitution Principle (LSP): The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. When a subclass refuses inherited methods, it violates the LSP, making it difficult to use the subclass as a true substitute for the parent class.

  • Misleading Design: When a subclass inherits methods from a parent class but doesn't use them or provides empty implementations, it can confuse developers who expect the inherited methods to work as intended.

  • Incomplete Behavior: Inherited methods are expected to provide certain behavior or functionality. If a subclass refuses the inherited methods, it might lack the necessary behavior or cause unexpected errors when those methods are invoked.

  • Maintenance Challenges: The subclass might need to be updated in the future due to changes in the parent class or to implement the missing behavior. This can lead to inconsistencies and increased maintenance effort.

  • Wasted Resources: Inheriting methods that are not used or are overridden with no-op implementations wastes memory, and development effort, and can complicate the understanding of the code.

  • Poor Code Reusability: The main purpose of inheritance is to promote code reusability. When a subclass refuses bequest, the reusability of the inherited methods is compromised.

Refactoring Recipe

To address this code smell, we can try the following refactoring skills:

  • Push Down Field

  • Push Down Method

  • Replace Inheritance with Delegation

Reference

https://refactoring.guru/smells/refused-bequest

https://ithelp.ithome.com.tw/articles/10212037

https://refactoring.guru/push-down-field

https://refactoring.guru/replace-inheritance-with-delegation

Last updated