title: Association, Composition and Aggregation - Implementation with java
body: <p>I'm a bit confused about <code>Association, Aggregation and Composition</code>. Even though loads of website, and forums discusses this topic, i have become more confused after reading some of them.</p>
  
  <p>I want to know if the following are correct :</p>
  
  <p>1.) Aggregation - WIll exist if the Whole is destroyed. for example an <code>Engine</code> could exist with or without a <code>Car</code>. </p>
  
  <p>2.) Composition - Will not exist if the object is destroyed. for example a <code>Room</code> can't exist without a <code>House</code>.</p>
  
  <p>3.) Association - I am not sure, at what instances we should use this. Can someone comment on this.</p>
  
  <p>When it comes to writing Java codes for Aggregation, COmposition and Association</p>
  
  <p>4.) <strong>Aggregation</strong></p>
  
  <pre><code>Class Car {
  
      private Engine engine;
  
      public void setEngine(Engine engine){
       this.engine=engine;
      }  
  
      public Engine getEngine(){
       return engine;
      }
  }
  </code></pre>
  
  <p>I think that if it's an Aggregation association, then there should be <code>accessors and mutators</code> defined. Is this correct ?</p>
  
  <p>According to my definition of Aggregation, i mentioned that if the <code>Car</code> object is destroyed, the <code>Engine</code> can be accessed. But, i don't see that hapenning in the above code. If Car object is destroyed, there will not be any way to access the Engine like <code>car.getEngine()</code>. So how does this happen ?</p>
  
  <p><strong>Composition</strong></p>
  
  <pre><code>Public House {
     private Room room;
     Public House (){
        room = new Room (int noRooms, String nameOfHouse);
  
     }
  }
  </code></pre>
  
  <p>I think that if it's a <code>C0mposition</code>, then there should not be any <code>accessor mutator</code> functions. and the Object <code>Room</code> has to be created only inside the Constructor. and  the instance method that holds the <code>room</code> object must be <code>Private</code> ? Am i correct ?</p>
  
  <p>note: Once again, i have googled, and visited many websites/forums trying to clear my doubts.Similar questions were posted in many forums, but yet i only got more confused about this. Can someone help me clear my doubts. Thank you!</p>