Inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support.
51
votes
14answers
10k views
Where does this concept of “favor composition over inheritance” come from?
In the last few months, the mantra "favor composition over inheritance" seems to have sprung up out of nowhere and become almost some sort of meme within the programming community. And every time I ...
32
votes
9answers
2k views
Code Smell: Inheritance Abuse
It's been generally accepted in the OO community that one should "favor composition over inheritance". On the other hand, inheritance does provide both polymorphism and a straightforward, terse way of ...
20
votes
4answers
6k views
Why should I prefer composition over inheritance?
I always read that composition is to be preferred over inheritance. A blog post on unlike minds, for example, advocates using composition over inheritance, but I can't see how polymorphism is ...
2
votes
1answer
335 views
Composition vs. Inheritance
Here's what is given:
public interface Request {}
// there are 20 subclasses of Request
public class CreateUserRequest implements Request {
@NotEmpty
public String userName;
}
// request ...
15
votes
3answers
6k views
Best design for Windows forms that will share common functionality
In the past, I have used inheritance to allow the extension of Windows forms in my application. If all of my forms would have common controls, artwork, and functionality, I would create a base form ...
9
votes
5answers
931 views
How do we know to favour composition over generalisation is always the right choice?
Whether an object physically exists or not, we can choose to model it in different ways. We could arbitarily use generalisation or composition in many cases. However, the GoF principle of "favour ...
7
votes
6answers
919 views
Why is subclassing too much bad (and hence why should we use prototypes to do away with it)?
I was reading up on design patterns, and I read that the prototype design pattern does away with excessive subclassing.
Why is subclassing bad? What advantage would using a prototype bring about over ...
10
votes
3answers
894 views
Inheritance vs mixins in dynamic languages?
When should you prefer inheritance patterns over mixins in dynamic languages?
By mixins, I mean actual proper mixing in, as in inserting functions and data members into an object in runtime.
When ...
2
votes
7answers
575 views
Inheritance using non-abstract base class
This post is based on the question http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance/11758048#comment15634305_11758048.
Some people said - check whether there is “is-a” ...
4
votes
2answers
292 views
Who decided on the terminology downcasting and upcasting?
As far as I know, the terminology comes from how the inheritance hierarchy is traditionally displayed, with the extending types at the bottom, and the parent types at the top.
This is a bit ...