CSC108 Lecture #12 - Multiple Inheritance


I've been asked several times about whether or not Java allows multiple inheritance. The simple answer is that it does not, but the more complicated answer is that Java provides features that allow us to simulate multiple inheritance.

We might envision a multiple inheritance hierarchy as follows:


            Truck          Car

               \           /
                \         /
                 \       /

                PickupTruck

It makes sense that we might need such a hierarchy because a PickupTruck might need to inherit some properties that are common to cars, and also may require other properties that are common to trucks.

This type of class configuration does have some problems though. What if the Truck and Car classes both contain a method having the same same (perhaps a "drive()" method). Which method should the PickupTruck class inherit?

Java only allows single inheritance. Instead it uses interfaces to provide some of the functionality that multiple inheritance supports. While a class can be derived from only one parent, it can implement many different interfaces. A pickup truck could be derived from a truck and implement a car interface, for example. Because interfaces can be derived from other interfaces, an entire alternative to a class hierarchy can be defined for an application (i.e., an interface hierarchy).


[ Home | Outline | Announcements | Newsgroup | Assignments | Exams | Lectures | Links ]


U of T IMAGE

© Copyright 2000. All Rights Reserved.