Liskov substitution principle (LSP) says:
Preconditions cannot be strengthened in a subtype.
In C#, I could violate 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 would happen if A.DoStuff
would be an abstract
method:
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.DoStuff
is contractless. Or its contract is just everything allowed.
So, is B.DoStuff
precondition violating Liskov substitution principle?