|
|
(4 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| ''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.''
| |
|
| |
|
| __TOC__
| |
|
| |
| == <b> Inheritance vs. Delegation </b> ==
| |
|
| |
| ===<b>Inheritance</b>===
| |
| 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.
| |
|
| |
| ====<b>Advantages and Disadvantages of Inheritance</b>====
| |
| Advantages of Inheritance: <br>
| |
| - Straightforward to use <br>
| |
| - Supported by many programming languages <br>
| |
| - Easy to implement new functionality <br>
| |
|
| |
| Disadvantages of Inheritance: <br>
| |
| - Exposes a subclass to the details of its parent class <br>
| |
| - Changes in the parent class implementation would cause the subclass to change as well <br>
| |
|
| |
|
| |
| ===<b>Delegation</b>===
| |
| 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.
| |
|
| |
| ====<b>Advantages and Disadvantages of Delegation</b>====
| |
| Advantages of Delegation: <br>
| |
| - Provides flexibility due to the fact that any object can be replaced at run time by another one of the same type <br>
| |
|
| |
| Disadvantages of Delegation: <br>
| |
| - Is efficient since objects are encapsulated <br>
| |
|
| |
| == <b> References </b> ==
| |
| [http://users.encs.concordia.ca/~hshong/comp6471/ch5-2.ppt Inheritance vs. Delegation]
| |