inheritance: is circle an ellipse?
Jan. 24th, 2011 11:46 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
It's a known puzzle from the depths of object-oriented teaching: is Circle a subclass or Ellipse. From school we know it is; but as we read in books, for programmers, it is not. How come? Oh, "Liskov principle". They use Liskov principle to scare kids.
Actually we know that a circle is an ellipse with both axes being the same length.
But in programming, an ellipse is something different. It is at least stretchable. And it is also moveable. Somehow it is not rotatable, but we can skip it for simplicity.
An ellipse in programming is (almost) always horizontal; it can be moved (by changing coordinates of its center) and it can be stretched/compressed by changing its height and width. Sure a stretchable ellipse is neither an ellipse nor a circle, not even a stretchable circle. A stretchable circle stretches equally in all directions.
That's it, no? Questions? Objections?
Actually we know that a circle is an ellipse with both axes being the same length.
But in programming, an ellipse is something different. It is at least stretchable. And it is also moveable. Somehow it is not rotatable, but we can skip it for simplicity.
An ellipse in programming is (almost) always horizontal; it can be moved (by changing coordinates of its center) and it can be stretched/compressed by changing its height and width. Sure a stretchable ellipse is neither an ellipse nor a circle, not even a stretchable circle. A stretchable circle stretches equally in all directions.
That's it, no? Questions? Objections?
no subject
Date: 2011-01-25 03:03 am (UTC)So if you had color-blind circles, then "colored circles" is a subclass (i.e. -- it adds an ability of a circle to know its color). Now, if your "colored circles" could never change color, then you could also make "red circles" a subclass of "colored circles", since you would not be removing any abilities. But if your "colored circles" could change colors before, then "red circles" is not a subclass.
Same analysis about "visible circles".
> "What can an ellipse do that circle cannot"?
If an ellipse object cannot change its shape (cannot change the radii independently, for example, via setters), then, indeed, a circle is a fine subclass of "ellipse". If ellipse radii were mutables, however, you would be taking away that ability, which is not allowed.