Composition in OOP
Presented by Huba Akhtar
Definition:Definition:
What is Composition?What is Composition?
Lets Suppose:Lets Suppose:
Let's say we must manage rooms in a hotel.
We will have a class Hotel that has several
objects Floor. And each floor is composed of
several Rooms.
Using "is composed" give us a hint on the
relationship between Floor and Room.
Continue…Continue…
If we happen to destroy the object Floor,
what should happen to the Rooms??
If a Floor is destroy then all the Rooms
inside should also be destroy. That is aThat is a
compositioncomposition
Example:Example:
For Example, you could have a class called
Band. The data member of Band class
could consist of objects from the Guitarist,
Drummer, and Vocalist classes. These
objects are data member of the Band class,
but not the parent classes. They are related
by composition, not by inheritance.
Composition in OOP

Composition in OOP

  • 1.
  • 2.
  • 3.
    What is Composition?Whatis Composition?
  • 4.
    Lets Suppose:Lets Suppose: Let'ssay we must manage rooms in a hotel. We will have a class Hotel that has several objects Floor. And each floor is composed of several Rooms. Using "is composed" give us a hint on the relationship between Floor and Room.
  • 5.
    Continue…Continue… If we happento destroy the object Floor, what should happen to the Rooms?? If a Floor is destroy then all the Rooms inside should also be destroy. That is aThat is a compositioncomposition
  • 6.
    Example:Example: For Example, youcould have a class called Band. The data member of Band class could consist of objects from the Guitarist, Drummer, and Vocalist classes. These objects are data member of the Band class, but not the parent classes. They are related by composition, not by inheritance.