Suppose we have trivial classes:
class A
{
virtual int Function(int number)
{
return number;
}
}
class B : A
{
override int Function(int number)
{
return number + 1;
}
}
class UseExample
{
void Foo(A obj)
{
A.Function(1);
}
}
Will this example be a violation of LSP ?. If so, could you give me an example that does not violate the principle and uses a different implementation?
How about this:
class B : A
{
int variable;
override int Function(int number)
{
return number + variable;
}
}
As I understand it, using the variable "variable" raises a stronger precondition and therefore violates the LSP. But I'm not quite sure how to follow LSP when using polymorphism.
source
share