Tuesday, January 22, 2008

Composition over Inheritance

Recently when walking through the design of some new features for an application, another developer asked: "Why should I use composition over inheritance?" I attempted to answer that question, and felt that I did not do as good of a job answering it as I would have liked. I was recently looking for something else on Phil Haack's blog and ran across this good article, Composition over Inheritance and other Pithy Catch Phrases.

I think that Phil does a very nice job of explaining the argument and the key point in why this question should be asked: "The goal is not to bend developers to the will of some specific patterns, but to get them to think about their work and what they are doing."  Please give this article a read for more details.


Anonymous said...

Theoretical example:

Base class: Person

Class Male inherits Person

Class Female inherits Person

"is a" relationship: Male "is a" Person; Female "is a" Person


Base class: Vehicle

Class Car inherits Vechicle

Class Jeep inherits from Vehicle

Same as above, Car & Jeep have "is a" relationship with Vehicle



Class Male "has a" Car

Class Female "has a" Car

Paige Cook said...

Thanks! I really like this example. Good way to show the relationship.