nicolas cellier wrote:
James Foster a écrit :
On Nov 25, 2008, at 7:45 PM, Igor Stasenko wrote:
My understanding of inheritance is different, in short: A subclass of particular class is a _specialization_ of base class, not _expansion_.
In this context, I sometimes wonder if Square should inherit from Rectangle (a specialization in which width and height are equal), or Rectangle should inherit from Square (adding an instance variable). Am I right that you would have Square inherits from Rectangle (Square being more specialized)? But then it feels like we are wasting an instance variable (since Rectangle would have two).
Beware, you're dangerously sliping to multiple inheritance because your Square might also be a lozenge :)
With the Rectangle as subclass of Square option this wouldn't be a problem - you would just have two different subclasses of Square.
Could Rectangle be a subclass of Square? Sure:
- Square instance variables: center, size, orientation
- Rectangle adds this instance variable: aspectRatio
We can make Ellipse a subclass of Circle using the same style. We can even have an #aspectRatio method in Square and Circle which always returns 1 and then we can move some of the more general code up in the classes hierarchy if we want to.
For raster graphics it is convenient to define Rectangles as always parallel to the screen axis so that just two points are enough to fully identify them. Perhaps calling them RasterRects instead would have made us think more clearly about them. We later moved to vector graphics (Balloon) but were stuck with the historic baggage.
-- Jecel P.S.: I am aware that even in this scheme you might want Parallelogram to inherit from both Rectangle and Lozenge (actually, Rhombus is more generic) and then you have the multiple inheritance problem again. Which is what Traits are for...