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

# Re: OO should break when broken(was re: the benefits of immutability)

```Quoting Colin Putney <cputney@wiresong.ca>:
> [...] Circle should be the superclass. A subclass should extend its
> superclass, not constrain it. An ellipse is a circle with additional
> capabilities - it can have different radii along each of its dimensions.
> [...]

Let's work on another example.

1. Derive the subtype relations between the types Real, Number, Complex,
and Integer?

2. Implement the number type hierarchy in your OO language of choice.

3. Based on the insights from the above exercises, should Ellipse be
inherited from Circle (implying that Ellipse is a subtype of Circle)?

Regards,
Vesa Karvonen

...

Only read the following hints below after you have given some thought to
the above problems.

See, for example, James O. Coplien's book Multi-Paradigm Design for C++
for further discussion.

Hints:

1.a. For each of the fuctions

greatest_common_divisor(x,y),
maximum(x,y), and
complex_square_root(x),

consider whether the function is well defined if all of the arguments
it is given are either Real, Complex or Integer.

1.b. Find the subset relations between the sets Real, Number, Complex and
Integer.

2. Use the Bridge pattern to separate the behavioral interface hierarchy
from the implementation hierarchy.

3. Assume you have inherited Ellipse from Circle. Can I write a function
on Circles that will not work correctly if it is passed an Ellipse?
Why would it be bad?

```