Solutions
Various different solutions can be tried to resolve this problem.
Here are some examples.
- 1.
- Make Number subclass of Magnitude, but redefine comparison operators in
class complex to give error message if used.
This is using subclassing for the purpose of limitation, something
we said was not a good idea in an earlier chapter.
- 2.
- Don't use inheritance at all - redefine all operators in all classes.
this is termed flattening the inheritance tree. Unfortunately, it
loses all the benefits of inheritance we have previously described.
- 3.
- Use part inheritance, but simulate others - use Number, but have each
number implement all relational operators. Again, this loses some of
the benefits of inheritance.
- 4.
- Make Number and Magnitude independent, and have Integer inherit from both.
This is what is termed multiple inheritance.