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

Re: OO should break when broken

On Thursday, September 4, 2003, at 06:28  AM, John Clements wrote:

>> Yes, what you say is true. But I don't see how you could characterize 
>> this as the subclass constraining the superclass. For every valid 
>> LinkedList, there is a valid LinkedListWithFastSize.
> Not if you consider elements of other possible subclasses of 
> LinkedList as 'valid LinkedLists', which by subtyping they ought to 
> be.  For instance, imagine a LinkedList subclass 
> LinkedListWithTimeStamp, which tracks when it was created.  Would you 
> claim that for each element of this subclass, there is a unique 
> LinkedListWithFastSize?

No. Obviously there isn't. Let me rephrase my claim: "For every valid 
instance of LinkedList, there is at least one valid instance of 
LinkedListWithFastSize." The same holds true for 
LinkedListWithTimeStamp: "For every valid instance of LinkedList, there 
is at least one valid instance of LinkedListWithTimeStamp." Was your 
objection to my loose usage of 'valid LinkedList,' or to my broader 
position on subclassing?

The point is that the full range of state that is valid in the 
superclass is also valid in the subclass. It isn't restricted, which 
Vesa claimed was "the essence of a subclass." Note that having Circle 
subclass Ellipse *does* restrict the state of the superclass. There are 
valid Instances of Ellipse that cannot be converted into valid 
instances of Circle. In contrast, any valid instance of LinkedList can 
be converted to a valid instance of LinkedListWithFastSize.

My argument goes like this:

1. The essence of a subclass is to extend it's superclass.
2. An ellipse is an extension of a circle.
3. Therefore, Ellipse should be a subclass of Circle.
4. Geometrically, a circle is a subtype of an ellipse
5. Therefore, the class hierarchy is inverted with respect to the type 
hierarchy in the domain
6. Therefore, circles and ellipses are not good examples for 
discussions of object modeling (except, perhaps, when the discussion is 
about the essence of subclassing. ;-)

I could make similar arguments about triangles or quadrilaterals.