CSC/ECE 517 Fall 2010/ch2 2f EC: Difference between revisions
No edit summary |
No edit summary |
||
Line 199: | Line 199: | ||
<br> | <br> | ||
In a multiple inheritance supporting language such as C++, the "CowDuckHybrid" class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use. | In a multiple inheritance supporting language such as C++, the "CowDuckHybrid" class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use. | ||
<br> | |||
<br> | |||
The code implementation in C++ illustrates this kind of problem below. | |||
<br> | |||
<br> | |||
<code> | |||
class Animal {<br> | |||
String makeSound () {<br> | |||
return "silence"<br> | |||
}<br> | |||
}<br> | |||
<br> | |||
class Cow: public Animal {<br> | |||
String makeSound() {<br> | |||
return "moo"; // redefine method<br> | |||
}<br> | |||
}<br> | |||
<br> | |||
class Duck: public Animal {<br> | |||
String makeSound() {<br> | |||
return "quack"; // redefine method <br> | |||
}<br> | |||
}<br> | |||
<br> | |||
class CowDuckHybrid: public Cow, public Duck {<br> | |||
// automatically inherit makeSound() from Cow and Duck<br> | |||
}<br> | |||
<br> | |||
// Main code<br> | |||
Animal a;<br> | |||
a.makeSound(); // illegal statement - compiler will complain<br> | |||
a.Cow::makeSound(); // outputs "moo"<br> | |||
a.Duck::makeSound() // outputs "quack"<br> | |||
<br> | |||
<br> |
Revision as of 19:50, 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.
Disadvantages of Multiple Inheritance
Although multiple inheritance gives programmers more flexibility in class hierarchies and 'mix-and-match' implementation, one would think that it should be included in all object-oriented languages.
The reality is that several implementation issues keep it from being incorporated into some languages.
Some key issues include name and method collision3, the diamond problem4, and conceptual difficulties and understandability problems3.
Name collision
A subclass that inherits from several parents classes with the same variable names will have name collisions.
Figure 5: Which x value will print_x() method print?
It is unclear which value of 'x' would be printed. Different languages and compilers will handle this differently.
Method collision
Similar to name collision, when a subclass inherits from the several parents with the same method names, method collision will occur.
Figure 6: Which show() method will Package inherit?
The Diamond Problem
The diamond problem occurs when the direct base classes are themselves derived from another class5. This can be illustrated in the hierarchy below. The pattern forms a diamond shape, hence the name of the problem.
Figure 7: What sound should the CowDuckHybird make?
Let's define two classes called "Cow" and "Duck". They both inherit from "Animal". The makeSound() method is inherited in both "Cow" and "Duck" classes. The "Cow" class defines the method to return "moo", while the "Duck" class redefines the method to return "quack".
Let's assume that through some strange breeding, a cow and duck breed to create a "CowDuckHybird" classes which inherits from both "Cow" and "Duck" parents. When the makeSound() method is called on a "CowDuckHybrid" object, what sound will make?
In a multiple inheritance supporting language such as C++, the "CowDuckHybrid" class would implement both makeSound() methods, and the programmer has to explicitly specify which one to use.
The code implementation in C++ illustrates this kind of problem below.
class Animal {
String makeSound () {
return "silence"
}
}
class Cow: public Animal {
String makeSound() {
return "moo"; // redefine method
}
}
class Duck: public Animal {
String makeSound() {
return "quack"; // redefine method
}
}
class CowDuckHybrid: public Cow, public Duck {
// automatically inherit makeSound() from Cow and Duck
}
// Main code
Animal a;
a.makeSound(); // illegal statement - compiler will complain
a.Cow::makeSound(); // outputs "moo"
a.Duck::makeSound() // outputs "quack"