Reputation: 636
i'am not sure about special case of LSP. Actually child should extend implementation of parent and not change it. For me means extension, that i add new lines in child implementation to parent implementation within child class. But what about, when i completely override parent-implementation and it provides result too?
Parent is:
public class Calculator {
public void computeProfit(){
int profit = 2+2;
}
}
Child is:
public class SpecialCalculator extends Calculator {
@Override
public void computeProfit(){
//overriding completely parent-implementation
int profit = Math.pow(2) * 5;
}
}
has LSP still been preserved?
Upvotes: 0
Views: 103
Reputation: 1593
TL;DR; In you example LSP is preserved. IMO the thing with SOLID principles it that it is a mnemonic for dynamic polymorphism so the end goal for your code is to enable something like this
public void SomeBusinessLogic(Calculator calc) {
calc.computeProfit() // SpecialCalculator might go here as well
}
Your code is perfectly fit for this.
This however would violate LSP
public class SpecialCalculator extends Calculator {
public void computeProfitClevrly(){
//overriding completely parent-implementation
int profit = Math.pow(2) * 5;
}
}
because now you'll have to do something like
public void SomeBusinessLogic(Calculator calc) {
if (calc is SpecialCalculator)
calc.computeProfitCleverly() // SpecialCalculator might go here as well
}
P.S. There are some more subtle cases of LSP violation. I.e. when base class is ImmutablePoint
and a child class is a point that might be mutated.
Upvotes: 1