Skip to main content

All Questions

Filter by
Sorted by
Tagged with
142 votes
5 answers
73k views

Why should I prefer composition over inheritance?

I always read that composition is to be preferred over inheritance. A blog post on unlike kinds, for example, advocates using composition over inheritance, but I can't see how polymorphism is achieved....
MustafaM's user avatar
  • 2,028
157 votes
15 answers
51k 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 ...
58 votes
9 answers
25k views

Code Smell: Inheritance Abuse [duplicate]

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 ...
dsimcha's user avatar
  • 17.3k
44 votes
2 answers
64k views

Why is inheritance generally viewed as a bad thing by OOP proponents [duplicate]

I keep hearing the phrase "Favour Composition over Inheritance" from GoF, which is being annoyingly mentioned repeatedly by my friend, who thinks it is a valid blanket statement. But is it ...
RonaldMunodawafa's user avatar
38 votes
3 answers
11k views

Is "composition over inheritance" violating "dry principle"?

For example, consider I have a class for other classes to extend: public class LoginPage { public String userId; public String session; public boolean checkSessionValid() { } } and ...
ocomfd's user avatar
  • 5,722
4 votes
1 answer
858 views

Composition vs. Inheritance [duplicate]

Here's what is given: public interface Request {} // there are 20 subclasses of Request public class CreateUserRequest implements Request { @NotEmpty public String userName; } // request ...
Andrey Agibalov's user avatar
13 votes
3 answers
6k views

Composition over inheritance but

I'm trying to teach myself software engineering and coming up against some conflicting information which is confusing me. I've been learning OOP and what abstract classes / Interfaces are and how to ...
MikeMason's user avatar
  • 319
8 votes
5 answers
1k 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 ...
Phil Helix's user avatar
  • 1,966
7 votes
7 answers
5k views

Inheritance using non-abstract base class

This post is based on the question https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance/11758048#comment15634305_11758048. Some people said - check whether there is “is-a” ...
LCJ's user avatar
  • 987
5 votes
6 answers
972 views

What is the correct OOP relation between complex and real numbers?

On one hand class complex { double real; double imaginary; } it is composition on the other hand class complex : double { double imaginary; } it is inheritance Which one is correct? ...
Dims's user avatar
  • 157
5 votes
2 answers
1k views

Design classes to model 3D scanned faces of ancient Greek/Roman sculptures: is multiple inheritance a good design solution?

I would like to deepen the topic of multiple inheritance using Python and I usually find examples that are too simple. I love art and I imagined the following problem and I want to understand if ...
blunova's user avatar
  • 398
3 votes
1 answer
361 views

Is this a misuse of "composition over inheritance"?

The general sentiment seems to be that composition is preferable, in most cases, over inheritance, as it leads to less coupling. But for a case like this, module Publishable def new? @content....
ivan's user avatar
  • 319
0 votes
1 answer
408 views

Should I force "composition over inheritance" rule to class members?

As I know, according to "composition over inheritance" rule, we should avoid reusing a method by inheritance, but how about class members? Suppose I have parent and child classes like the following: ...
ggrr's user avatar
  • 5,813
0 votes
2 answers
356 views

Inheritance versus Composition in a business application

I have a training management and tracking system, with a high level structure as follows: We have a Role1, e.g. Manager, Shift-boss, miner, etc. and a Candidate, training for that Role. The role has ...
ProfK's user avatar
  • 502