This post originated from an RSS feed registered with .NET Buzz
by Jonathan Crossland.
Original Post: inheritance over composition
Feed Title: Jonathan Crossland Weblog
Feed URL: http://www.jonathancrossland.com/syndication.axd
Feed Description: Design, Frameworks, Patterns and Idioms
Favour composition over inheritance ?
Well actually we are comparing apples with oranges.
With regards to maintenance, composition still provides objects, still provides encapsulation, and other useful rewards.
When you call objectA.Method() and it forwards a call to objectB.Method() (composition) you have just as easy maintenance, as you have deriving from objectB.Method()
But apples are not oranges, we are actually talking of two distinct features and usages.
A car has a door - composition
A car is a vehicle - inheritance
In the composition example, a door is a Type, which is used within the Car Type. - Car.Door
In composition you are likely to "distinguish" objects, where inheritance helps avoid it.
Take the same examples, but where the Door becomes a superclass, you have inheritance which provides something more to the pure compositional example.
Car.Door where CarDoor -> Door
Favouring composition over inheritance is a good guideline, but not a winner by much of a margin and of course its mixing metaphors.
It depends on how you construct the two concepts together, that makes the whole picture better or worse.
Remember the construct has vs is.
It is more important to get the relationships between objects correct, which if it is, will automatically provide better maintainability because it will make sense.