CSC/ECE 517 Fall 2010/ch2 2f EC: Difference between revisions
No edit summary |
No edit summary |
||
Line 57: | Line 57: | ||
}<br> | }<br> | ||
</code> | </code> | ||
</p> | |||
<h2>Multiple Inheritance</h2> | |||
<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". | |||
<br> | |||
<br> | |||
[[Image:EC_fig2.jpg]] | |||
<br> | |||
Figure 2: Case of multiple inheritance | |||
<br> | |||
<br> | |||
The case where a subclass inherits from multiple parent classes is called <b>multiple inheritance</b>. This feature is not supported in all objected oriented languages. | |||
<br> | |||
<br> | |||
Languages that support multiple inheritance include C++, Python, Perl, Lisp, Tcl2, and Ruby (with the use of mixins). | |||
<br> | |||
<br> | |||
The C++ implementation of the example is shown below. | |||
<br> | |||
<br> | |||
<code> | |||
class Frame {<br> | |||
// frame of a bike<br> | |||
}<br> | |||
<br> | |||
class Wheels {<br> | |||
// wheels of a bike<br> | |||
}<br> | |||
<br> | |||
class Bike: public Frame, public Wheels<br> | |||
// create bike by putting together frame and wheels<br> | |||
}<br> | |||
<br> | |||
</code> | |||
</p> |
Revision as of 19:31, 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
}