Does adding a method to a sub-class with the same name of the parent class method, break substitutability principle?
https://softwareengineering.stackexchange.com/questions/415823
-
15-03-2021 - |
Question
Let T
be a superclass, and S
derives from it:
abstract class T{
public virtual byte[] Foo(string str){...}
}
class S : T{
public override byte[] Foo(string str){....}
public byte[] Foo(string str, bool b){...}
}
As per the LSP, if:
S s = new S();
var data = s.Foo("dt", false);
I should be able to substitute S
with T
:
T t = new S();
var data = t.Foo("dt", false); //can't call this.
Does this design break LSP?
Should I, in this case, change the name of the method in the derived class?
Solution
I should be able to substitute
S
withT
No. You should be able to substitute T
with S
.
Does this design break LSP?
No, it doesn't. Any client that only knows about T
will not even now that the second overload exists, so it will never try to call it, and thus there will be no change in behavior.
Licensed under: CC-BY-SA with attribution
Not affiliated with softwareengineering.stackexchange