Does adding a method to a sub-class with the same name of the parent class method, break substitutability principle?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/415823

문제

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?

도움이 되었습니까?

해결책

I should be able to substitute S with T

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 softwareengineering.stackexchange
scroll top