1 / 15

Aggregation vs Composition

Aggregation vs Composition. Introduction. In normal terms, they both refer to member object but the survival or existence of th e member object makes the differences. Aggregation.

colum
Download Presentation

Aggregation vs Composition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Aggregation vs Composition

  2. Introduction • In normal terms, they both refer to member object but the survival or existence of the member object makes the differences.

  3. Aggregation • Aggregation is also known as a 'has a' relationship because the containing object has a member object and the member object can survive or exist without the enclosing or containing class or can have a meaning after the lifetime of the enclosing object also.

  4. Aggregation Continues • Example:Room has a table and the table can exist without the room. The table can have meaning without the room also.

  5. Aggregation Continues

  6. How to implement Aggregation in coding • How can we manage that?

  7. Implement Aggregation in coding • public class Address{ . . .} • public class Person • { •      private Address address; •      public Person(Address address) •      { • this.address = address; •      } •      . . . • }

  8. Implement Aggregation in coding • Address address = new Address();Person person = new Person(address); • or • Person person = new Person( new Address() );

  9. Composition • The member object is a part of the containing class and the member object cannot survive or exist outside the enclosing or containing class or doesn’t have a meaning after the lifetime of the enclosing object. 

  10. Composition Continues • Example :Computer Science Department is a part of the College. The Computer Science Department cannot exist without the college and the department has no meaning after the lifetime of the college.

  11. Composition Continues

  12. How to implement Composition in coding • How can we manage that?

  13. Implement Composition in coding • public class Engine{ . . . } • public class Car • { •     Engine e = new Engine(); •     ....... • }

More Related