All Questions
Tagged with inheritance composition
71 questions
5
votes
2
answers
2k
views
How to maintain encapsulation with composition in C++?
I am designing a class Master that is composed from multiple other classes, A, Base, C and D. These four classes have absolutely no use outside of Master and are meant to split up its functionality ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 {...
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 ...
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 ...
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 ...
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 ...
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 ...
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,...
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 ...
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 ...
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 ...
-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 ...
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 ...
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? ...
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/...
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 ...
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 ...
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 ...
1
vote
2
answers
480
views
Inheritance vs 'specification' by fields
I'm currently reading 'Head first design patterns' and I already have a few questions on the first chapter of the book.
This chapter introduces the 'prefer composition over inheritance' design ...
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 ...
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 ...
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 ...
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 ...
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 */ ...
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” ...
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 ...
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 {} //...
-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 ...
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 ...
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:
...
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 ...
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....
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...