[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: OO should break when broken
On Thursday, September 4, 2003, at 01:28 PM, Guy Steele - Sun
Microsystems Labs wrote:
>> Unless somebody wants to post code in an OO language showing an
>> implementation of Circle as a useful subclass of Ellipse, this is my
>> last post in this thread.
>
> Ellipse.java:
> class Ellipse extends Object {
> float long_axis;
> float short_axis;
>
> public Ellipse(float long_axis_init, float short_axis_init)
> {
> long_axis = long_axis_init;
> short_axis = short_axis_init;
> }
> }
>
> Circle.java:
> class Circle extends Ellipse {
>
> public Circle(float radius)
> {
> super(radius, radius);
> }
> }
>
>
> Very nice, but let
>
> float radius = 3;
> Ellipse e = new Ellipse(radius, radius);
>
> and then, alas,
>
> e instanceof Circle
>
> is false.
Not only that, it's pointless to have the subclass at all. You get the
same effect with this:
Ellipse.java:
class Ellipse extends Object {
float long_axis;
float short_axis;
public Ellipse(float long_axis_init, float short_axis_init)
{
long_axis = long_axis_init;
short_axis = short_axis_init;
}
public Ellipse(float radius)
{
long_axis = radius;
short_axis = radius;
}
}