Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Possible Duplicate:
Where does this concept of “favor composition over inheritance” come from?

I have colleagues at work who claim that "Inheritance is an anti-pattern" and want to use composition systematically instead, except in (rare, according to them) cases where inheritance is really the best way to go.

I want to suggest an alternative where we continue using inheritance, but it is strictly forbidden (enforced by code reviews) to use anything but public members of base classes in derived classes.

For a case where we don't need to swap components of a class at runtime (static inheritance), would that be equivalent enough to composition? Or am I forgetting some other important aspect of composition?

share|improve this question

marked as duplicate by pdr, Steven Jeuris, Walter, Caleb, Eric King Nov 12 '12 at 19:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Related discussion on Stack Overflow: Prefer composition over inheritance? –  Steven Jeuris Nov 12 '12 at 17:04
1  
    
If you systematically avoid inheritance everywhere, your code will not be OOP and the benefits of it will be lost on you. That being said, some people don't like OOP, and maybe that's who you're working with. –  MrFox Nov 12 '12 at 18:20
1  
@Frank: Ok, I see what you're saying, but I would strongly recommend reading all the answers to the various conversations above. You probably need to understand the benefits of composition over inheritance before you start fighting that decision. Make no mistake, it is a bad decision to completely forbid inheritance but it's also a bad decision to use inheritance before you fully understand the reasons not to. –  pdr Nov 12 '12 at 19:16
1  
@MrFox That's not quite true. OOP is about interfaces. Inheritance is just a mechanism that happens to be included in most OOP languages. –  Doval Mar 28 at 11:53

2 Answers 2

up vote 7 down vote accepted

You're sort of looking at it backwards. Composition isn't preferred because of some unseen benefit of composition, it's preferred because it avoids the drawbacks of inheritance. Inheritance adds the significant benefit of substitutability. However, that benefit comes with the significant cost of tight coupling. Inheritance is the strongest coupling relationship possible. If you don't need the substitutability, the coupling cost isn't worth it.

Your restricted version of inheritance addresses its other cost, a loss of encapsulation, but does nothing about the much more significant cost of coupling.

share|improve this answer
1  
Thanks Karl, that's the kind of thing I was looking for. –  Frank Nov 12 '12 at 18:38
    
This may be the best explanation of this concept I've ever seen. –  Magus May 13 at 16:12

I think the difference between the two things is in their intent. Is the class you are writing really meant to be used as replacement / specialisation of its Base class or is it just using some functionality? If the First Case is true you should go with inheritance. If the latter is true go for composition. In my experience using inheritance where composition would be appropriate creates a more complex, less understandable and meanigful codebase.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.