views:

135

answers:

5

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?

+7  A: 

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.

Cylon Cat
And Aggregation is a ... relationship.
Henk Holterman
Aggregation 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.
Cylon Cat
A: 

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.

w69rdy
+4  A: 

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

FrustratedWithFormsDesigner
+3  A: 

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.

Carl Manaster
+1: Composition is king.
Alex Humphrey
A: 
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.

Zaki