Skip to main content

Composition in Java with code examples


Composition 


"Have a " Relationship  OR  "Must have" relationship                    between two or more classes.
Container class contains component's class object
For example a  Car is composed of engine and body. Where Car is a container class and both Engine and Body are components class. 
Life Time Issue in Composition


  • Life time of component objects depends on container class
  • If Container object destroyed, component objects will be also destroyed


Car Example 



  • There is a composition relationship between car's body and engine.
  • Car must have Engine and body
  • If Car destroyed, both Engine and Body will be destroyed



  1. class Engine{
  2.  Engine () {   System.out.println("Engine created");}
  3.  void start() {
  4.   System.out.println("Engine Start");
  5.  }
  6. }
  7. class Body{
  8.  Body () {System.out.println("Body created");}
  9.  void type() {
  10.   System.out.println("Saloon Type");
  11.  }
  12. }
  13. class Car{
  14.  Engine e;
  15.  Body   b;
  16.  Car () { e=new Engine();
  17.           b= new Body();
  18.          System.out.println("Car created");
  19.  }
  20.  void specification(){
  21.    e.start();
  22.    b.type();
  23.  }
  24. }
  25. class Post{
  26. public static void main(String a[]){
  27. Car c= new Car();
  28. c.specification();
  29. }
  30. }


Output
Engine created
Body created
Car created
Engine Start
Saloon Type

Same problem with Data Members


Example : Create car(price) class as container object with engine(hp) and body(size) as component objects




  1. class Engine{
  2. int hrp;
  3. Engine () { }
  4. Engine(int hp) { hrp=hp;}
  5. int getHrp() { return hrp;}
  6. void setHrp(int h){hrp=h;} 
  7. void start() {
  8. System.out.println("Engine Power "+hrp);
  9. }
  10. }
  11. class Body{
  12. int size;
  13. Body()  {}
  14. Body(int s){size=s;}
  15. void setSize(int s){size=s;}
  16. int getSize(){ return size;}
  17. void type() {
  18. System.out.println("Saloon Size "+size);
  19. }
  20. }
  21. class Car{
  22. Engine e;
  23. Body b;
  24. int price;
  25. Car () { e=new Engine();
  26.          b= new Body();
  27. }
  28. Car (int p,int hp,int s) { 
  29. e=new Engine(hp);
  30. b= new Body(s); 
  31. price=p; 
  32. }
  33. void specification(){
  34.  e.start();
  35.  b.type();
  36.  System.out.println("Car Price "+price);
  37. }
  38. }
  39. class Post{
  40. public static void main(String a[]){
  41. Car c= new Car(40000,800,5);
  42. c.specification();
  43. }
  44. }




Output:
Engine Power 800
Saloon Size 5
Car Price 40000


 Example : Create  4  cup of tea and serve into tray.
CupofTea consist of milk, sugar, Teabag and water.

Container  Class :
  CupofTea

Component Class :

   Milk (qty)
   sugar(spoone)
   Teabag(make)
   Water(qty)
Tray : 
 Array of cupoftea




  1. class Milk {
  2.  private int qty;
  3.  public Milk ()  {}
  4.  public Milk (int q)  {qty=q;}
  5.  public void setQty(int q)  {qty=q;}
  6.  public int getQty(){ return qty;}
  7. }
  8. class Sugar{
  9.  private int spoone;
  10.  public Sugar ()  {}
  11.  public Sugar (int q)  {spoone=q;}
  12.  public void setSpoone(int q)  {spoone=q;}
  13.  public int getSpoone(){ return spoone;}
  14. }
  15. class Teabag{
  16.  private String make;
  17.  public Teabag ()  {}
  18.  public Teabag (String q)  {make=q;}
  19.  public void setMake(String q)  {make=q;}
  20.  public String getMake(){ return make;}
  21. }
  22. class Water{
  23.  private int qty;
  24.  public Water ()  {}
  25.  public Water (int q)  {qty=q;}
  26.  public void setQty(int q)  {qty=q;}
  27.  public int getQty(){ return qty;}
  28. }
  29. class CupOfTea{
  30. private Milk m;
  31. private Sugar s;
  32. private Teabag t;
  33. private Water w;
  34. public CupOfTea() {
  35.           m=new Milk();
  36.           s=new Sugar();
  37.           t=new Teabag();
  38.           w= new Water();
  39. }
  40. public CupOfTea(int q,int s1, String m1, int w1){
  41.           m=new Milk(q);
  42.           s=new Sugar(s1);
  43.           t=new Teabag(m1);
  44.           w= new Water(w1);
  45. }
  46. public void setMilk(int q){m.setQty(q);}
  47. public void setSugar(int t){s.setSpoone(t);}
  48. public void setTeabag(String q){t.setMake(q);}
  49. public void setWater(int q){w.setQty(q);}
  50. public int getMilk(){return m.getQty();}
  51. public int getSugar(){return s.getSpoone();}
  52. public String getTeabag(){return t.getMake();}
  53. public int getWater(){return w.getQty();}
  54. }

  55. class Post {
  56. public static void main(String o[]){
  57.  CupOfTea tray[] = new CupOfTea[4];
  58.  CupOfTea  c1=new CupOfTea(5,1,"Lipton",3); 
  59.  CupOfTea  c2=new CupOfTea(5,10,"Lipton",3); 
  60.  CupOfTea  c3=new CupOfTea(5,0,"Olpers",3); 
  61.  CupOfTea  c4=new CupOfTea(5,15,"TarakChai",3); 
  62.  tray[0]=c1;
  63.  tray[1]=c2;
  64.  tray[2]=c3;
  65.  tray[3]=c4;
  66.  for (int i=0;i<tray.length;i++){
  67.     System.out.println("Cup no = " + i);
  68.    System.out.println("Milk="+tray[i].getMilk());
  69.    System.out.println("sugar="+tray[i].getSugar());
  70.    System.out.println("Teabag="+tray[i].getTeabag());
  71.     System.out.println("water="+tray[i].getWater());
  72.   System.out.println("...........................");
  73.  }
  74. }
  75. }

Output:

Cup no = 0
Milk=5
sugar=1
Teabag=Lipton
water=3
...........................
Cup no = 1
Milk=5
sugar=10
Teabag=Lipton
water=3
...........................
Cup no = 2
Milk=5
sugar=0
Teabag=Olpers
water=3
...........................
Cup no = 3
Milk=5
sugar=15
Teabag=TarakChai
water=3
...........................

Comments

  1. Hmmm, Nice Codes. Surely Will Help Other As It Did To Me. Keep Sharing Something Like This...

    ReplyDelete
  2. AoA
    i see yur site its so informatic site , i appriciate yur working
    thanx for sharing yur efforts

    ReplyDelete

Post a Comment

Popular posts from this blog

Java Basics Quick Review with examples

Java Programming Language High Level Strongly Typed means no automatic type conversions in java like C++ Case Sensitive Successor of c++ Pure Object Oriented Language  Hello World Problem in java class Test{ public static void main(String a[]) { System.out.println("Hello World");  } } More than one Classes class Test1{ void print () { System.out.println("Test class"); } } class Test{ public static void main(String a[]) { Test1 t=new Test1(); t.print();  } } Rule of Thumb: Don't write extra functions in driver class  Extra function and data in driver class  class Test{ int sq(int p){    return p*p; } public static void main(String a[]) {     Test t = new Test(); System.out.println(t.sq(4));  } } .................................. call without object instantiation .................................. class Test{ static int sq(int p){    return p*p; } public static voi...

Association in Java Programming

What is Association in java Relationship between one or more classes Aggregation and composition is a special type of association For example,  Student Belongs to a specific Faculty and  One faculty has many students Code Example: class  Student  {    String name;    Student  () { name= null ;  }    Student  ( String n ) { name=n;  }    void  setName ( String n ) { name=n; }    String getName () {  return  name; } } class  Faculty {    Student student [] ;    int  size;    Faculty  ( int  s ){ size= 0 ;             student= new  Student [ s ] ; } void  belongsTo ( Student p ){      student [ size ] =p;      size++; } void  prin...