What is a failure?

Can someone explain what a Denial of Answer means? I tried to read some articles and say that it’s a kind of code smell or in a wiki, he says that this is a class that redefines the method of the base class so that the base class contract is not executed by the derived class.

But in a nutshell or in simpler terms, what is it really?

+7
lsp solid-principles duck-typing
source share
1 answer

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() { //... } protected double addPersonalTax(double tax) { //... } public double getTax() { double tax = computeBaseTax(); return addPersonalTax(tax); } } 

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() { //... } @Override public double getTax() { double tax = computeInitialTax(); return addPersonalTax(tax); } } 

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.

+14
source share

All Articles