What is composition?

1 answers

Composition is exactly like Aggregation except that the lifetime of the ‘part’ is controlled by the ‘whole’. This control may be direct or transitive. That is, the ‘whole’ may take direct responsibility for creating or destroying the ‘part’, or it may accept an already created part, and later pass it on to some other whole that assumes responsibility for it.

Sample class Car is shown below to demonstrate Composition of tires, doors, windows and steering.

Car class

public class Car 
{  
  private Tire[] tires;
   
  private Door[] doors;
   
  private Steering steering;
   
  private Window[] windows;
}
 
class Tire
{
   
}
   
class Door
{
   
}
 
class Steering
{
   
}
 
class Window
{
   
}
shilpigupta22 shilpigupta22 [Author] 4 months ago votes 0