CSC/ECE 517 Fall 2010/ch2 2f EC: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 59: Line 59:
</p>
</p>


<h2>Multiple Inheritance</h2>
<h1>Multiple Inheritance</h1>
<p>
<p>
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class "Bike" could have been made up from two classes called "Frame" and "Wheels".  
To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class "Bike" could have been made up from two classes called "Frame" and "Wheels".  

Revision as of 19:38, 21 September 2010

What is inheritance?

Inheritance is a functionality of object-oriented programming where a subclass obtains the contents and functionality of its superclass.

Instead of duplicating a class, a user can create a class that is a subclass of another class, inherit all of its functionality, and just add additional functionality.

For example, if the class "Bike" already exists and we wanted to create specific type of bicycle such as "MountainBike" or "RoadBike", we could just create classes that inherit from the "Bike" class and add the additional functionality we need.


Figure 1: Simple case of inheritance

The use of inheritance is convenient because we did not have to duplicate code to create a new subclass and increases code re-usability1. Also, if changes are made to the superclass, those changes will be reflected in the subclasses as well.

Single Inheritance

For the example above, where a subclass inherits from a single parent class, this is called single inheritance.

Object-oriented languages including Java, support single inheritance. A Java implementation would appear as below.

public class Bike {
// Implementation of generic bike class
// ie. frame, wheels
}

public class MountainBike extends Bike {
// inherit generic bike components
// add shocks, fat tires, etc.
}

public class RoadBike extends Bike {
// inherit generic bike components
// add narrow tires, drop-style handlebars, etc.
}

Multiple Inheritance

To enhance the concept of code re-usability even further, a user may wish to inherit from multiple parent classes. For example, the class "Bike" could have been made up from two classes called "Frame" and "Wheels".


Figure 2: Case of multiple inheritance

The case where a subclass inherits from multiple parent classes is called multiple inheritance. This feature is not supported in all objected oriented languages.

Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins).

The C++ implementation of the example is shown below.

class Frame {
// frame of a bike
}

class Wheels {
// wheels of a bike
}

class Bike: public Frame, public Wheels
// create bike by putting together frame and wheels
}

Advantages of Multiple Inheritance

Multiple inheritance allows the programmer to 'mix-and-match' elements from several classes to form a new class. This saves effort from the programmer by not having to duplicate code and modify code when parent classes are changed. For more complicated hierarchies, this feature can quite useful.

Consider the case below where we have "Engine", "Chassis", and "Body" parent classes and two subclasses "Sedan" and "Truck".

The "Sedan" class is a sedan and needs an engine, chassis, and body so it inherits from all three parent classes. Same can be applied for the "Truck" class. The "Sedan" and "Truck" inherits all three components and they can modify them to suit their needs. The sedan could have a longer body to increase passenger room, the truck overrides body for an open cab, and so on.

Assume that we need polymorphic functionality. For example, we need to be able to run the following code:

Sedan s = new Sedan();

Then we pass object 's' to a method somewhere requiring an "Engine" type such as

int testEngine(Engine e);

Since our sedan has an engine we want to do:

testEngine(s);

Let's say later, we want to create a push-type lawnmower. The lawnmower does not need the chassis or body implementations, but needs an engine so we just inherit from the "Engine" class.


Figure 3: Multiple inheritance allows more flexible hierarchies.

With single inheritance, the structure illustrated above would not be possible. Since a subclass cannot inherit from multiple parent classes, the programmer would probably have to combine all three "Engine", "Chassis", and "Body" classes into a single class, perhaps called "Assembly". Then the "Sedan" and "Truck" classes would inherit from this combined class.


Figure 4: Single inheritance limits hierarchy.

The "Lawnmower" class now posses a dilemma. Should we inherit from the "Assembly" class and get a lot of functionality we don't need and possibly introduce unwanted behavior. Or should the code for the engine be duplicated from the "Assembly" class and copied into the "Lawnmower" classes. What happens when engine specifications change, we would need to modify engine code in two places?

Therefore, as the example demonstrates the structure for single inheritance is more constrained.