2

As a beginner, I have been learning about composition and inheritance. I realized that when using composition you do not create objects but a composition. I am a bit confused here. Lets say that we are building a human with a head, 2 arms and 2 legs.

Normally, what I would do is create arms legs and head classes and then create the object human which are build up by those. But in composition, you instead build a human class which are linked to other classes? Where am I wrong?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Huzo
  • 1,652
  • 1
  • 21
  • 52

6 Answers6

4

Composition:

class Human {
  Head head;
  Arm leftArm, rightArm;
  Leg leftLeg, rightLeg;
}

You create a composition if you use objects (like Head) in another class.

Inheritance:

class Woman extends Human {
}
M. Haverbier
  • 383
  • 2
  • 13
  • Yes, but I am not asking about the difference of these. I am trying to understand the relation between objects and composition. – Huzo May 30 '17 at 09:23
  • The example shows quite well how composition works - there is (or should be...) an instance of `Head`, two instances of `Arm` and `Leg`... given that those objects are instanciated correctly via `head = new Head()` (etc) - what don't you understand about this? – vikingsteve May 30 '17 at 09:26
3

Composition Example : Consider example of a Car and an engine that is very specific to that car (meaning it cannot be used in any other car). This type of relation ship between Car and SpecificEngine class is called Composition. Object of Car class cannot exist without object of SpecificEngine class and object of SpecificEngine has no significance without Car class. To put in simple words Car class solely "owns" the SpecificEngine class.

Aggregation Example : Now consider class Car and class Wheel. Car needs a Wheel object to function. Meaning Car object own Wheel object but we cannot say Wheel object has no significance without Car Object. It can very well be used in a Bike, Truck or different Cars Object.

Watto
  • 41
  • 2
3

Indeed, you are wrong.

Composition is often considered as an alternative to inheritancy but in both cases you manipulate instances of classes and not only classes as you suppose for the composition case.

With your example, a very bad example of inheritancy would be having :

  • a TwoLegs class that is two Legs.
  • a TwoArmsAndTwoLegs class (subclass of TwoLegs) that is two arms but also two legs.
  • a HeadAndTwoArmsAndTwoLegs class (subclass of TwoArmsAndTwoLegs) that is a head but also two arms and two legs.
  • a Human class (subclass of HeadAndTwoArmsAndTwoLegs) that is an human but also all the remains.

It is not flexible as all classes are strongly related in their hierarchy and the definition itself of their structure.

Here, composition would be to have a Human class that owns 5 fields :

  • a rightLeg field of the Leg class
  • a leftLeg field of the Leg class
  • a leftArm field of the Arm class
  • a rightArm field of the Arm class
  • a head field of the Head class
davidxxx
  • 125,838
  • 23
  • 214
  • 215
2

When Neo (from the Matrix) learns Kung-Fu by downloading it into his brain, it's called composition (you give an object a protocol to conform to, or 'connect' different other objects to it). But Neo would not have Trinity's eye color, since they are not related (it's possible only in the Matrix, not in real life).

If Neo and Trinity would've had a child, she wouldn't know Kung-Fu (unless later she's explicitly introduced to that same download as Neo), but would still have Trinity's eyes. That's inheritance.

SimpleBeat
  • 747
  • 1
  • 10
  • 20
1

Composition : Since Engine is-part-of Car, the relationship between them is Composition. Here is how they are implemented between Java classes.

public class Car {
    //final will make sure engine is initialized
    private final Engine engine;  

    public Car(){
       engine  = new Engine();
    }
}

class Engine {
    private String type;
}

Aggregation : Since Organization has Person as employees, the relationship between them is Aggregation. Here is how they look like in terms of Java classes

public class Organization {
    private List employees;
}

public class Person {
    private String name;   
}
Watto
  • 41
  • 2
0

is-a relationship is an Inheritance and has-a Relationship is a Composition.

class Manager extends Employee{ } // This is called Inheritance because all Manager is an Employee.

class Manager extends Employee{ Team team = new Team();

}

here manager has a team to manage so this comes under composition.(has-a relationship)

Anand Sinha
  • 322
  • 3
  • 7