Questions tagged [composition]

Composition means to assemble simpler elements into more complex structures. In OOP, composition usually refers to object composition, i.e. assembling several simpler objects into a a more complex aggregate. It may also refer to an aggregation of object, where the aggregate owns some components.

Composition means to assemble simpler elements into more complex structures. In OOP, composition usually refers to object composition, i.e. assembling several simpler objects into a a more complex aggregate. It may also refer to an aggregation of object, where the aggregate owns some components.

See also:

170 questions
155
votes
15 answers

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 see it, I'm a little bit mystified. It's like…
Mason Wheeler
  • 82,151
  • 24
  • 234
  • 309
136
votes
5 answers

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. But I have a feeling that when people say…
MustafaM
  • 1,958
  • 3
  • 15
  • 13
58
votes
9 answers

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 delegating everything to a base class unless…
dsimcha
  • 17,224
  • 9
  • 64
  • 81
50
votes
7 answers

Why is inheritance bad in a Person-Student model?

I've just started learning about Inheritance vs Composition and it's kind of tricky for me to get my head around it for some reason. I have these classes: Person class Person { public string Name { get; set; } public int Age { get; set; } …
40
votes
2 answers

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

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 not more sensible to consider that the real reason why…
RonaldMunodawafa
  • 661
  • 1
  • 6
  • 12
39
votes
2 answers

What is the meaning of "doesn't compose"?

I see a lot of texts, especially functional programming texts, claim that certain CS concepts "don't compose". Examples are: locks don't compose, monads don't compose. I've been having a hard time tracking down exactly the meaning of this phrase.…
38
votes
3 answers

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 some subclasses: public class HomePage extends…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
36
votes
5 answers

How is defining that a method can be overridden a stronger commitment than defining that a method can be called?

From : http://www.artima.com/lejava/articles/designprinciples4.html Erich Gamma: I still think it's true even after ten years. Inheritance is a cool way to change behavior. But we know that it's brittle, because the subclass can easily make…
q126y
  • 1,713
  • 3
  • 14
  • 24
18
votes
11 answers

Need Good OOP Design For World and Countries Problem

I am currently working on problem with a chicken or egg first situation. Basically, I am designing a solution which goes like this: World is a collection of countries; Each Country has a name, flag and president (all are strings); There will be…
15
votes
2 answers

How do I avoid writing lots of pass-through functions in a wrapper?

I have a class, which wraps another class of a common base type. Because the base type interface is quite large this involves writing a lot of pass-through functions. I am looking for a way to avoid this. Let's make an example: Car / …
Sarien
  • 751
  • 4
  • 13
13
votes
8 answers

"Prefer composition over inheritance" - Is the only reason to defend against signature changes?

This page advocates composition over inheritance with the following argument (rephrased it in my words): A change in the signature of a method of the superclass (which hasn't been overridden in the subclass) causes additional changes in many …
Utku
  • 1,922
  • 4
  • 17
  • 19
13
votes
9 answers

Should a developer know the inner workings of the computers' hardware?

I'm not talking just how memory is assigned and memory management (things that you can learn from C for example) but rather the hardware aspect and how each component of the computer hardware works internally and how they communicate with each…
Adrian
  • 147
  • 1
  • 3
13
votes
3 answers

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 use them, but then I'm reading that one should…
MikeMason
  • 319
  • 2
  • 7
13
votes
4 answers

Reducing boilerplate in class that implements interfaces through composition

I have a class: A that is a composite of a number of smaller classes, B, Cand D. B, C, and D implement interfaces IB, IC, and ID respectively. Since A supports all the functionality of B, C and D, A implements IB, IC and ID as well, but this…
Nick Udell
  • 1,214
  • 2
  • 11
  • 17
13
votes
4 answers

Is dependency injection by hand a better alternative to composition and polymorphism?

First, I'm an entry level programmer; In fact, I'm finishing an A.S. degree with a final capstone project over the summer. In my new job, when there isn't some project for me to do (they're waiting to fill the team with more new hires), I've been…
1
2 3
11 12