CSC/ECE 517 Summer 2008/wiki2 8 jb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 17: Line 17:
<ul>
<ul>
<li>Critics of inheritance argue that the protected access modifier breaks encapsulation by letting a derived class peer inside of its base class [http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance] </li>
<li>Critics of inheritance argue that the protected access modifier breaks encapsulation by letting a derived class peer inside of its base class [http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance] </li>
<li>
interface inheritance
</li>
</ul>
</ul>


Line 28: Line 31:
<li>[http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance] </li>
<li>[http://en.csharp-online.net/Classes,_Structs,_and_Objects%E2%80%94Delegation_and_Composition_vs._Inheritance] </li>
</ul>
</ul>
interface inheritance


==Links==
==Links==

Revision as of 03:41, 21 June 2008

This wiki will explore the age old debate of inheritance vs. delegation, showing the strengths and weakness of each approach, and where each approach is preferred.

Background

An in-depth description Inheritance and Delegation is out of the scope of this wiki, but a brief discussion of each approach will help get us started.

Inheritance

Inheritance is one of the fundamental tenets of object oriented programming. Inheritance refers to the ability to model hierarchies classes that are related to each other through the is-a relationship. It is commonly agreed upon that inheritance done correctly must conform to the Liskov substitution principle.

Delegation

Delegation, sometimes referred to as aggregation, is the concept that one class may contain an instance of another class, and delegate some responsibility to that class. This is also referred to as the has-a relationship. Aggregation is closely related to composition. Both aggregation and composition are used to describe one object containing another object, but composition implies ownership [1]. Aggregation is more general and doesn't imply any responsibilities for memory management. The class which contains other classes is called a composite class, while a class being contained is called a composited or composed class [2].

The Debate

Inheritance breaks encapsulation

  • Critics of inheritance argue that the protected access modifier breaks encapsulation by letting a derived class peer inside of its base class [3]
  • interface inheritance

Inheritance increases coupling

  • Critics of inheritance argue that inheritance can lead to brittle code due to coupling between base and derived classes [4]. When using delegation there are limited and explicit dependencies between the composite and composed classes. The interface of the composed class is the entire contract between the two classes. With inheritance the dependencies become blurred with multiple factors leading to close coupling. Access to base class members, unanticipated consequences of executing virtual methods, etc... all contribute to a more brittle relationship

Inheritance is static and determined at compile time

Links

Wikipedia: Inheritance
Wikipedia: Delegation
Wikipedia: Composition
Replace Inheritance With Delegation design pattern
Dr. Dobbs: Composition vs. Inheritance
Good debate
Great article covering delgation and composition
http://www.artima.com/designtechniques/compoinh.html
http://www.artima.com/lejava/articles/designprinciples4.html