I've already shown these in previous examples Brian. Either
a) perimeters aren't important with respect to how you want to use these
obects, or
Would anything relating to the "squareness" of the object be
important? If so then the given class hierarchy will fail to produce
the desired result for any operation that takes advantage of the
unique "squareness" of the object. If not then why have a Square
object to begin with.
b)
return a.Side[0] + a.Side[1] + a.Side[2] + a.Side[3]
In the example the Square class only contains a single scalar value to
represent its dimensions.
You obviously wouldn't use a design like that if you were going to inherit
Rectangle from Square, use 1 variable to represent a side, and calculate
perimeter. You've stacked the deck to answer the question the way you want.
Think outside the box and consider other designs/uses.
That was the example in the blog. I crafted the question to prove
that Square objects cannot be substituted for Rectangle objects.
Ask the OP. Presumably so he could substitute rectangles for squares, I
don't know.
Well if that were the case then the OP would have derived square from
rectangle instead of the other way around.
It's certainly easy enough for me to come up with a design that
works either way. If you're designing a system that uses vehicles, do you
inherit a bicycle from PassengerVehicle, or from ManualPoweredVehicle, or
from Vehicle, or from VehicleThatCanStayUprightWithoutMoving? I don't know,
it depends on what you're trying to model and which attributes are
priorities to you. If perimeters aren't important to you, then who cares
about how you store variables to calculate them?
In regards to the vehicle analogy I could derive BicycleVehicle from a
lot of different valid superclasses. But, I certainly wouldn't derive
it from UnicycleVehicle which is what the blogger would have me do
since a bicycle contains an additional wheel's worth of information.
Why? Because a bicycle is not a unicycle nor can a unicycle be
substituted for a bicycle.
The guy who wrote the article linked to had a point - things that have more
information in them tend to go lower in the hierarchy.
Yep, and more importantly, that would be the blogger's focus when
designing the class hierarchy which I completely disagree with. The
focus should be on whether the subclass logically represents an "is-a"
relationship with the superclass. That way whatever operation you
conceive that can be applied to the superclass can also be correctly
applied to the subclass. That is, the superclass can be substituted
for the subclass. If you adhere to that OO principal then the "is"
operator will be sufficient in most cases.