Chapter 13 - Mulitple Inheritance
- Incomparable Complex Numbers
- Rationale for "needing" or wanting multiple inheritance
- C++ is the only language being discussed that supports multiple
inheritance of classes
- Java has multiple inheritance of interfaces NOT classes
- Multiple inheritance is used when a derived class inherits from two or
more base classes.
- Walking Menus
- Name Ambiguity
- Name Ambiguity means that at least two of the base classes have a
method with the same name. Since the child inherits BOTH, which
one is to be used. (Example of draw() in Budd).
- The method name ends up being overloaded in the child class.
- Rename one of the methods. (change one draw() to be drawCard())
and simply call the correct "parent" draw() to help maintain the
interface.
- Redefine the methods - change what the method does.
- Often a combination of the 2 are used to handle the ambiguity
- Inheritance From Common Ancestors
- Still have to deal with the same name ambiguity problems but get
an additional problem in that the parent classes EACH have a copy
of all the data and methods in the common ancestor so the child
gets 2 copies of the grandparent stuff. One copy from each of its
parents.
- Occasionally, we want both copies of the data fields but most of the
time we don't.
- Also, if the grandparent uses constructors/initializations that
should only be done once. The grandchild will do them twice. Not
good.
- How to fix??
- Mulitple Inheritance in C++
- Example shows how to deal with the problem of name ambiguity.
- To handle the multiple copies problem with inheriting from common
ancestor, the problem is in the grandchild but the "FIX" has to be
in the parents. They must use virtual inheritance so that the
grandchild only gets 1 copy of the grandparent. See example pg. 247
in Budd.
- The "problem" in the child but the "fix" in the parent shows up
frequently in C++.
- Note: a virtual base class (i.e. a class that is inherited virtually)
is always initialized once and is done BEFORE anything else is done.
- Mulitple Inheritance in Java