wrote:
Hello, If I have an abstract class Bird and subclasses Sparrow, Pigeon and
Ostrich having methods fly(), with no-op for the Ostrich fly()
implementation. I am violating the LSP. Is this violation acceptable?
Is there any case where the violation is acceptable?
My Reply:
It's up to you. LSP is helpful in programming because it let's programmers more confidently reason about programs because
they can assume they know how something will behave. LSP is
not a constraint in the real world, it is a constraint that
can be put on software to help build systems.
One root of the problem is that types in most programming
languages are based on classical categorization. The real
world is much more interesting and may combine examplar and
prototype based categorization
(http://www.bsos.umd.edu/hesp/newman/Newman_classes/Newman300/webpages/categorization.pdf).
A tree stump can be considered on the outer boundry
of being a seat, but it's not our best example of a seat. And
in fact the only reason a tree stump could be considered a
seat at all is because we are human. In the classification
system of an ant or an elephant, a tree stump probably wouldn't
be in the seat category.
This sort of ambiguity is not easily represented in class structures.
The categories we think of as natural and intrinsic to the world are
largely the production of our human embodied self's relation with
the world.
In the same way categories in your system need to be related to the
problem space. The problem space is what embodies your solution
and acts as the context in which questions, definitions,
and relationships are resolved.
If you wish to build a software system then following LSP is probably
a good idea. Figure out why you care about flight and make all your
objects consistent with that view of the world. Trying to model reality in an unsuitable medium like a programming language can
get very frustrating. Perhaps the brain is the complexity needed.
You should let the problem you are trying to solve guide what things
mean. Leave the platonic realms for philosophers.
7:59:28 PM