Skip to main content

All Questions

Filter by
Sorted by
Tagged with
1 vote
5 answers
677 views

How does inheritance lead to higher coupling than composition?

One major reason for using composition over inheritance is, that inheritance leads to higher coupling. How is that? In both cases members are exposed to the subclass (in case of inheritance) or ...
cobby's user avatar
  • 119
2 votes
2 answers
419 views

Composition over inheritance: how data are accessed in the composition case?

I've been reading this Wikipedia article Composition over inheritance. It gives a code example of inheritance first, and then a code example of composition. In case of inheritance there are data that ...
Andrew Savinykh's user avatar
0 votes
2 answers
1k views

Inheritance/Composition VS "Direct Injection Construction"

my following example seems to go into the direction Inheritance VS Composition. But that's not, what i want to ask. I see the concept Inheritance and Composition on one side and the alternative, which ...
Robin Kreuzer's user avatar
50 votes
7 answers
10k views

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 {...
Octavian Niculescu's user avatar
0 votes
2 answers
555 views

How to solve an issue when a decorator needs variables from the base class?

I have a service class that does some magic. I want to introduce a new type of functionality - raise an event. I am absolutely sure that decorator pattern is great for this scenario. The problem is ...
pro100tom's user avatar
  • 449
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
9 votes
6 answers
3k views

Composition over inheritance when adding functionality to a foreign object

Note: I am not sure of the correct terminology for what I am describing, so if you have suggestions for a better title please feel free to edit it. "Favor composition over inheritance" is ...
Asad-ullah Khan's user avatar
0 votes
1 answer
163 views

Composition without direct instantiation through constructor

I have 2 classes that independently operate on specific file formats: class FileWriterA and class FileWriterB. Each class has some shared methods (e.g., __enter__, __exit__, __str__, closed, is_closed,...
pstatix's user avatar
  • 1,047
4 votes
3 answers
646 views

Inheritance vs composition: How would you implement an "unordered list"? Subclass of list, or composition?

This came up at work and left me thinking about the best way to model this: In Python, we have the built-in list container, which is a mutable sequence. Equality between two lists is defined as ...
Lagerbaer's user avatar
  • 556
3 votes
2 answers
177 views

Composing and Inheriting from the Same Type

To start off with an example: I have a read-only Repository used for getting arbitrary values. This behavior can be implemented multiple ways. I also want to allow opt-in mutation of the repository's ...
Mario Ishac's user avatar
-4 votes
1 answer
208 views

Alternatives to inheritance for polymorphic behavior for data in database?

I am thinking of designing a website where people can enter their aliases for games they played in the past and they can search for others around the same time period. FWIW I don't care if it exists ...
Vishaal Kalwani's user avatar
0 votes
1 answer
426 views

favor composition over inheritance: practical problems

In a C# application, I've got a behaviour that I would like to be available in different classes that not necessarily share the same ancestor. What better opportunity to 'favour composition over ...
AgostinoX's user avatar
  • 841
18 votes
11 answers
10k views

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 ...
csavvy's user avatar
  • 345
4 votes
1 answer
2k views

Delegate vs Forwarding in Java OOP

I'm reading some article about "prefer composition over inheritance", and heard about Forwarding and Delegation. After search for the different I found some source: https://en.wikipedia.org/wiki/...
nhoxbypass's user avatar
9 votes
9 answers
2k views

Inheritance vs Composition For Chess Pieces

A quick search of this stackexchange shows that in general composition is generally considered more flexible than inheritance but as always it depends on the project etc and there are times when ...
CanISleepYet's user avatar
0 votes
1 answer
78 views

Inheritance or composition for a more advanced implementation of a class?

So i have a class called VirtualMouse, it is used to perform mouse actions such as moving and clicking. public class VirtualMouse { public VirtualMouse() { } public void ...
Joao Vitor's user avatar
2 votes
6 answers
2k views

Should I still prefer composition over inheritance if the child classes need BOTH the parent interface AND its class properties?

According to Why should I prefer composition over inheritance?, I should prefer composition over inheritance. But what if I need to access the interface and class member in generic way? For example, I ...
ocomfd's user avatar
  • 5,722
5 votes
4 answers
14k views

Why the industry prefer/use composition over inheritance? [duplicate]

I was having a discussion with few friends about inheritance and composition and what I learnt from that discussion is that the use of inheritance more or less condemned in industry nowadays and ...
Diaufh138's user avatar
0 votes
0 answers
34 views

How to organize classes which should reflect different versions of standard?

I have some standard which changes in time. Let's say I have std_v1, std_v2 and std_v3. Those standards have a lot of common parts with some little differences, possible additions and deletions. On ...
Viacheslav Kondratiuk's user avatar
1 vote
3 answers
377 views

Strategy Pattern and inheriting boilerplate functionality

I've read that when using the Strategy Pattern, you should stick to composition rather than inheritance. But does this mean that any inheritance at all is bad practice? If there is functionality to be ...
Allie Fitter'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
3 votes
1 answer
224 views

To inherit or to override?

Imagine that I am writing a game where tanks fight with each other. A generic Tank class is created and has the method fire() which fires a cannon, looks like this Tank::fire() { /* fires a cannon */ ...
Sunny Pun's user avatar
  • 139
0 votes
1 answer
66 views

How to represent self-composed implementations in inheritance hierarchies

Background: My company is working with camera systems. In the beginning we built our application around different models/setups of them, which i.e. consisted of different cameras or procedures to ...
user1709708's user avatar
6 votes
2 answers
935 views

OOP without Inheritance [closed]

I'm making a language, and I'm considering disallowing inheritance. To be clear, this means: class A {} //Valid interface B{} //Valid class A2 extends A {} //Invalid class B2 implements B {} //...
Nathan Merrill's user avatar
-1 votes
1 answer
142 views

What would be the best option in this scenario?

I have a statement. I know the ups and downs of both Composition and Inheritance, but I am unable to realize that what would be the best option the given scenario. SCENARIO: A Software company is ...
Khubaib Khawar's user avatar
13 votes
8 answers
6k views

"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 ...
Utku's user avatar
  • 1,922
4 votes
4 answers
3k views

Composition over Inheritance, why not both?

I have this out of context scenario, where what I think is good practices leaves me in a situation of both implementing an interface, and using composition to do the implementation. Imagine the ...
Chris Wohlert's user avatar
10 votes
1 answer
788 views

Is there a situation where it would be better to use weak references instead of simple composition?

Although the Java docs specify, that Weak references are primarily for canonicalizing mappings, you will find many, many, many people on the internet stating, that the WeakHashMap is perfect for ...
Leprechaun's user avatar
4 votes
2 answers
444 views

Would you use object proxy, extend the class, if else conditions or make a duplicate class for supporting two different API's?

This is not an easy one but here goes. I'm working on adapting a JavaScript project (ACE Editor) to support two different targets but maybe more. Any way I look at it, it looks like a large task and ...
1.21 gigawatts's user avatar
7 votes
3 answers
422 views

Composition of two classes with common inheritance root

Hope your day is going well. I want to make the following composition of two objects with the same inheritance root: There will be 3 basic operations on the Wallet class: add, getCards and ...
ovnia's user avatar
  • 461
2 votes
5 answers
175 views

Inheritence structure to allow unknown types

Let's say I have a client that is consuming Vehicle objects sent from a server. I have two types of vehicles: Car and Truck. Some Cars can be inHybridMode but not all of them. The server currently ...
tir38's user avatar
  • 203
1 vote
1 answer
136 views

Code reuse on function write objects data to file

I have a few base, derived classes and need to dump their data to file. The difference is only a field, but I don't know how to reuse code of base class. Sorry if noob question :p Those classes look ...
Sanna Jo's user avatar
0 votes
1 answer
119 views

Abstract class vs relation between classes

Searching for simplest solution where a User can order an item. If someone purchases an item he will have one address. Which then is the invoice and shipping address. But he could add another address ...
caramba's user avatar
  • 141
2 votes
3 answers
160 views

Base Class vs Common Member Class

Imagine I have two classes like this... public class SingleplayerGameSetup { public int Seed { get; private set; } public int AiNations { get; private set; } } public class ...
starikcetin's user avatar
6 votes
5 answers
1k views

Is "too much methods" a valid reason to break the "composition over inheritance" rule?

I usually prefer to follow "composition over inheritance" rule, until I start stepping in the world of Java GUI. In my experience, for adding custom requirements in a GUI components, for example, add ...
ggrr's user avatar
  • 5,813
2 votes
1 answer
369 views

Can composition be used in unit/integration testing over inheritance

In larger projects, there can often be a group of tests that test the functionality of related classes. These tests often share a lot of setup and preparation functionality. What happens then is ...
Thijs Riezebeek's user avatar
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
4 votes
2 answers
2k views

Applying composition over inheritance to Vehicle classes

I have a car maintenance garage program that has an abstract Vehicle class that has several derived classes like Car MotorCycle, etc. Each of those derived vehicles in turn is either a fuel or an ...
shinzou's user avatar
  • 444
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
4 votes
6 answers
484 views

Can we say "If a parent class never appears in my codes except in its child class, it should be composition instead of inheritance"?

I read some posts about "composition over inheritance","where to use composition/inheritance" , "Is-a relationship..." or "Liskov substitution principle" for some time, but I am not sure if I get the ...
ggrr's user avatar
  • 5,813
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
36 votes
5 answers
3k views

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 ...
q126y's user avatar
  • 1,733
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
11 votes
5 answers
700 views

Should I prefer composition or inheritance in this scenario?

Consider an interface: interface IWaveGenerator { SoundWave GenerateWave(double frequency, double lengthInSeconds); } This interface is implemented by a number of classes which generate waves of ...
Aviv Cohn's user avatar
  • 21.5k
1 vote
1 answer
385 views

Inheritance or Composition?

Let's say I have a main class A and another class B. In the B class the user can change some settings and variables of the program that belongs to A, I want to be able to apply these changes to the A ...
Amen's user avatar
  • 135
0 votes
1 answer
618 views

refactor by inheritance or composition for large class that depend on common state [duplicate]

I have a huge class (>1000 lines), in which all methods depend on a small set of attributes in that class. I can think of 2 quick ways of refactoring. I keep it as a huge object, but split related ...
funklute's user avatar
  • 201
3 votes
1 answer
818 views

Compose nested EventEmitters elegantly

I am developing a server-side JavaScript tool, and trying to do so in a modular way. I have a Parser class that is an EventEmitter - you call parse and it will eventually emit data and end events. It ...
Brandon Horst's user avatar
-1 votes
2 answers
719 views

what is the difference between inclusion and inheritance [duplicate]

the question is self explanatory, I just want to know when to use the inclusion and when to use inheritance, and which one serves for re-usability. in other words, which one meets the Object ...
younes's user avatar
  • 123
8 votes
1 answer
3k views

Inheritance, Composition and Protocols

I'm reading a book on general object oriented concepts to try and gain a solid understanding of everything. One thing that is throwing me off is the difference between Inheritance and Composition. I ...
Henry F's user avatar
  • 211
2 votes
1 answer
567 views

Should I use composition or inheritance for sharing configuration items?

I'm working on a fairly new project where I'd like to share some config items used by a JavaScript plugin. Specifically, the project is using jQuery dataTables and I want to find a good way of sharing ...
Jimmy Breck-McKye's user avatar