[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OO should break when broken(was re: the benefits of immutability)




On Monday, September 1, 2003, at 05:19  AM, Steven Shaw wrote:

> I don't think an ellipse is a kind of circle. It would make more sense 
> for a
> circle to be a kind of ellipse. The problem seems to be that the 
> "extends"
> relationship doesn't mean the same thing as "is-a", "a-kind-of" or
> "a-specialisation-of".

That's why I think shapes are a lousy example. Subclasses are subtypes, 
and do have a is-a, a-kind-of relationship with their parents. The 
problem is that geometric types are defined differently than class 
types.

Shapes are defined in terms of constraints. To create a subtype, you 
add constraints. So a quadrilateral is a polygon with four sides, a 
rectangle is a quadrilateral with right angles at the vertices, a 
square is a rectangle with sides of equal length. Classes types are 
defined in terms of state. To create a subclass you add state. (We'll 
ignore behaviour for now.)

To model a shape with a class you have to include enough state to cover 
the range of variation allowed by the constraints. The more specific 
the geometric subtype, the more constraints it has, and the less state 
is required to model it. So the type hierarchies of shapes and the 
classes that model them have an inverse relationship. That's why all 
circles are ellipses, but all Ellipses are Circles.

A better example might be Linnean taxonomy. If you had an app that 
simulated an ecosystem, you might end up with a class hierarchy of 
living things that resembled taxonomic classifications. Elephant would 
be a subclass of Mammal, for example. You'd have to have a fairly 
detailed simulation before that would be necessary though.

Colin