CSC/ECE 517 Fall 2007/wiki2 10 ab: Difference between revisions
No edit summary |
No edit summary |
||
Line 12: | Line 12: | ||
==Inheritance or Delegation== | ==Inheritance or Delegation== | ||
The following contents refers to the debates on in which conditions inheritance would be a better approach and in which delegation would be better. As given in [http://www.perlmonks.org/index.pl?node_id=278375] | The following contents refers to the debates on in which conditions inheritance would be a better approach and in which delegation would be better. As given in [http://www.perlmonks.org/index.pl?node_id=278375 "http://www.perlmonks.org/index.pl?node_id=278375 | ||
"] | |||
[1], it mentions that the choice between inheritance and delegation would depend on the linguistic interpretation of the application. As given in the site a class car can inherit from vehicles and hence it would inherit all functionality that a vehicle has. But a car class would not inherit from a wheel class since a car is not a wheel, rather it is composed of wheels and hence it can delegate the functionality of the wheel operations to the wheel class. As a general rule it can be said that for a Is-A relationship between the derived and base class it would be safe to use inheritance , while delegation would be suitable for implementing a Has-A relationship. | [1], it mentions that the choice between inheritance and delegation would depend on the linguistic interpretation of the application. As given in the site a class car can inherit from vehicles and hence it would inherit all functionality that a vehicle has. But a car class would not inherit from a wheel class since a car is not a wheel, rather it is composed of wheels and hence it can delegate the functionality of the wheel operations to the wheel class. As a general rule it can be said that for a Is-A relationship between the derived and base class it would be safe to use inheritance , while delegation would be suitable for implementing a Has-A relationship. | ||
A more clear representation of the Is-A and a Has-A relationship with examples is given in this page [2]. The following C++ code take n from [2] has been used . | A more clear representation of the Is-A and a Has-A relationship with examples is given in this page [2]. The following C++ code take n from [2] has been used . |
Revision as of 23:10, 23 October 2007
Problem Statement
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.
What is Inheritance
Inheritance in the concept of Object Oriented Programming is the process by which one object can inherit or acquire the properties of other objects. With the use of inheritance each object which are instances of derived classes can only define the properties which are unique to itself and derive qualities or common attributes from other objects which are instances of base classes .The obvious advantages of inheritance is reusability which greatly reduces code repetition. Inheritance is directly supported by object oriented languages and it gives way to the concept of polymorphism. The inheritance relationship between the base and derived classes is illustrated at compile time which means that a class cannot change its parent class dynamically at run-time.
What is Delegation
Delegation, in the concept of object oriented programming refers to the ability of an object in assigning or delegating the implementation of certain functions to other objects. This concept is also referred to as dynamic inheritance. As compared to inheritance delegation is not directly supported by object oriented languages but on the other hand unlike inheritance , the class to which the function is delegated can be changed at runtime.
Inheritance or Delegation
The following contents refers to the debates on in which conditions inheritance would be a better approach and in which delegation would be better. As given in [http://www.perlmonks.org/index.pl?node_id=278375 "http://www.perlmonks.org/index.pl?node_id=278375 "] [1], it mentions that the choice between inheritance and delegation would depend on the linguistic interpretation of the application. As given in the site a class car can inherit from vehicles and hence it would inherit all functionality that a vehicle has. But a car class would not inherit from a wheel class since a car is not a wheel, rather it is composed of wheels and hence it can delegate the functionality of the wheel operations to the wheel class. As a general rule it can be said that for a Is-A relationship between the derived and base class it would be safe to use inheritance , while delegation would be suitable for implementing a Has-A relationship. A more clear representation of the Is-A and a Has-A relationship with examples is given in this page [2]. The following C++ code take n from [2] has been used .
Below is a mouth class which has a eat function class Mouth { public: void eat(Food bite); }; To illustrate inheritance the person class extends the functionality of the mouth class and hence it has the eat function too. class Person: public Mouth { };
With the idea of delegation the Person class would delegate the eat function to the Mouth class as is illustrated here. class Person { public: void eat(Food bite) { itsMouth.eat(bite); } private: Mouth itsMouth; };
If we consider the question if a Person is a Mouth and if a Person has a Mouth , it can be seen which principle should be implemented
Examples in which Inheritance may be better
As referred in [3], inheritance should be utilized carefully and should be used when overall classes have the same behavior and overall differ by limited number of methods and plus they should satisfy the Is-A relationship.
Examples in which Delegation may be better
Again referring to [3], a very good example may be provided of a case where the concept of inheritance has not been utilized well.
References
[1] http://www.perlmonks.org/index.pl?node_id=278375