CSC/ECE 517 Fall 2007/wiki2 10 cv
Inheritance vs. delegation. Follow the debate on inheritance vs. delegation. Construct (or, better, cite) examples that show cases where inheritance is better, and cases where delegation is better. Attempt to characterize the situations in which you should employ one or the other.
Inheritance
Inheritance is a way to form new classes in object oriented programming using already defined classes. Inheritance is used to help reuse code that already exists with little or no modifications and this is one of the advantages of using inheritance since it helps reduce the complexity of a program. MAny programmers are confused on when to use inheritance but inheritance is mainly used when a programmers new class can be used in place of the existing class and the relationship between them can be described as an is-a relationship.
Advantages and Disadvantages of Inheritance
Advantages of Inheritance:
- Straightforward to use
- Supported by many programming languages
- Easy to implement new functionality
Disadvantages of Inheritance:
- Exposes a subclass to the details of its parent class
- Changes in the parent class implementation would cause the subclass to change as well
Delegation
Delegation is when one object relies on another object in order to provide a specified set of functionalities and this is sometimes referred to as aggregation, consultation, or forwarding. It is better to use delegation when a programmers new class needs to use the existing class and the relationship can be described as a has-a or uses-a relationship.
Advantages and Disadvantages of Delegation
Advantages of Delegation:
- Provides flexibility due to the fact that any object can be replaced at run time by another one of the same type
Disadvantages of Delegation:
- Is efficient since objects are encapsulated
Examples of Delegation and Inheritance
The following section discusses the examples explaining the usage of Inheritance and Delegates and how they can be used in specific situations. Also, it talks about the pros and cons of using these models by taking a common example.
Where Delegation makes more sense than Inheritance
The Game Example
Consider a game with different difficulty levels. Now suppose we have two kind of player levels smart and dumb. Here the requirement is that it is possible to change the difficulty level of the game at any time. We can construct two models to solve this problem, one with inheritance and the other with delegation.
In model A, the client of the computer player much know when to create to create the instances of an appropriate class when switching from difficult to simple mode or vice versa. The trouble here is that in Inheritance, once the object instance is created its type can not be changed at later time. Inheritance allows polymorphism, which provides a kind of run-time flexibility, but this model is undesirable. Client code to change the difficulty level at run-time is very complicated, and requires detailed knowledge of the computer player’s implementation classes.
On the other hand in model B, it is possible for class ComputerPlayer to have a simple setDifficulty() method. This can be done using delegation and at runtime using this method we can chance the difficulty level of the game. Therefore, delegation is a dynamic relationship, providing a different kind of runtime flexibility in the code, compared to inheritance.