The Liskov Signature Principle (LSP) says:
Preconditions cannot be strengthened in a subtype.
In C #, I could break the whole principle as follows:
public class A
{
public virtual void DoStuff(string text)
{
Contract.Requires(!string.IsNullOrEmpty(text));
}
}
public class B : A
{
public override void DoStuff(string text)
{
Contract.Requires(!string.IsNullOrEmpty(text) && text.Length > 10);
}
}
But what happens if there A.DoStuffis abstract:
public class A
{
public abstract void DoStuff(string text);
}
public class B : A
{
public override void DoStuff(string text)
{
Contract.Requires(!string.IsNullOrEmpty(text));
}
}
Now A.DoStuffis limitless. Or his contract is all that is allowed.
So, B.DoStuffdoes the premise violate the Liskov substitution principle?
source
share