I think you get the point. Refused Bequest is the smell of code. But, what type of code smell? Quoting Martin Fowler's book Refactoring: Improving the Design of Existing Code :
Subclasses inherit the methods and data of their parents. But what if they do not want or do not need what they are given? They are given all these great gifts and choose only a few to play with.
You have a subclass that inherits from the parent class, but the subclass does not need all the behavior provided by the parent class. Because of this, the subclass refuses any behavior (will) of the parent class. That is why it is the smell of code.
Update response to @catzilla comment:
If you donβt have the opportunity to read a book (I fully recommend it), at least you have a SourceMaking page that describes this pretty well.
Sample code, try. Imagine that we have classes for calculating taxes per person. We may have a class that calculates state taxes:
class Government { protected double computeBaseTax() {
Then we can have a class that calculates the amount of money that the company must pay as taxes. For some reason, we realized that this class can reuse the addPersonalTax method, but not computeBaseTax() . And making a bad decision, we decided that our Company class is inherited from Government .
class Company extends Government { private double computeInitialTax() {
Well, the problem can be solved better (override the computeBaseTax() method), but I'm trying to show that Refused Bequest is the smell of code that happens when we inherit a base class and some functionality is provided.
rchavarria
source share