2008/11/26 James Foster Smalltalk@jgfoster.net:
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).
Good example. Right , Square looks more specialized. And since it always having width == height , it looks like an error to keep a redundant state. So, i don't see much problem. We can define a common ancestor, like TwoDimensionalObject, having #width, #height abstract accessors. And then, define two subclasses Square and Rectangle which have different specialization of TwoDimensionalObject because require a different number of slots for storing data.
There are many examples of such approach. Consider a Number - Float - BigInteger classes. Float and BigInteger sharing common behavior which comes from Number but storage format is completely different. Same is for String - ByteString and WideString. This actually shows that inheritance is not relevant with objects format.
James