What is composition as it relates to object oriented design?
Solution 1
Composition refers to combining simple types to make more complex ones. In your example, composition could be:
Animal:
Skin animalSkin
Organs animalOrgans
Mammal::Animal:
Hair/fur mammalFur
warm-blooded-based_cirulation_system heartAndStuff
Person::Mammal:
string firstName
string lastName
If you wanted to go totally composition (and get rid of all inheritance) it would look like this:
Animal:
Skin animalSkin
Organs animalOrgans
Mammal:
private Animal _animalRef
Hair/fur mammalFur
warm-blooded-based_cirulation_system heartAndStuff
Person:
private Mammal _mammalRef
string firstName
string lastName
The advantage to this approach is that the types Mammal
and Person
do not have to conform to the interface of their previous parent. This could be a good thing because sometimes a change to the superclass can have serious effects on the subclasses.
They still can have access to the properties and behaviours of these classes through their private instances of these classes, and if they want to expose these former-superclass behaviours, they can simply wrap them in a public method.
I found a good link with good examples here: http://www.artima.com/designtechniques/compoinh.html
Solution 2
Composition is simply the parts that make up the whole. A car has wheels, an engine, and seats. Inheritance is a "is a " relationship. Composition is a "has a" relationship.
Solution 3
There are three ways to give behavior to a class. You can write that behavior into the class; you can inherit from a class that has the desired behavior; or you can incorporate a class with the desired behavior into your class as a field, or member variable. The last two represent forms of code reuse, and the final one - composition - is generally preferred. It doesn't actually give your class the desired behavior - you still need to call the method on the field - but it puts fewer constraints on your class design and results in easier to test and easier to debug code. Inheritance has its place, but composition should be preferred.
Solution 4
class Engine
{
}
class Automobile
{
}
class Car extends Automobile // car "is a" automobile //inheritance here
{
Engine engine; // car "has a" engine //composition here
}
Composition - Functionality of an object is made up of an aggregate of different classes. In practice, this means holding a pointer to another class to which work is deferred.
Inheritance - Functionality of an object is made up of it's own functionality plus functionality from its parent classes.
As to why composition is preferred over inheritance, take a look at the Circle-ellipse problem.
Solution 5
An example of Composition is where you have an instance of a class within another class, instead of inheriting from it
This page has a good article explaining why people say "favour composition over inheritance" with some examples of why.
![DaveDev](https://i.stack.imgur.com/NQuFs.jpg?s=256&g=1)
DaveDev
Updated on July 05, 2022Comments
-
DaveDev almost 2 years
I hear (and read on this site) a lot about "favour composition over inheritance".
But what is Compositon? I understand inheritance from the point of Person : Mammal : Animal, but I can't really see the definition of Compostion anywhere.. Can somebody fill me in?
-
Henk Holterman almost 14 yearsAnd Aggregation is a ... relationship.
-
Cylon Cat almost 14 yearsAggregation may be simple composition, or if it is a collection of like things (e.g., wheels on a car), it may be treated as a collection. A car can have four individual wheels, uniquely identified, or it can have a collection of wheels. It depends on usage. If using a collection class, then the collection is itself an aggregation.
-
thesummersign over 12 yearsSo can I say that: "Composition is when I create an object of
Class A
insideClass B
(instead of subclassingClass B
fromClass A
)." ? -
thesummersign over 12 yearsoh. ok got it
w69rdy
answered it later. YES I can say that. -
Alois Mahdal about 10 yearsNot really an instance of a class[C2] within another class[C1] but an instance of a class within instance of another class. The former could be misunderstood as if you instantiated C2 when defining C1, which should not be common.
-
als0052 almost 2 yearsOk this makes sense now. For other python programmers who are trying to understand this don't get caught up in the idea that "everything in python is an object" when thinking about composition. Instead think "when I make my class I will use other classes I made as instance variables"