Understanding the Difference Between Aggregation and Composition in Object-Oriented Programming

Slide Note
Embed
Share

Aggregation and Composition are two important concepts in object-oriented programming. Aggregation refers to a 'has-a' relationship where the contained object can survive independently, while Composition indicates that the member object is part of the containing class and cannot exist separately. This guide explores examples and implementation of both concepts in coding.


Uploaded on Sep 26, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

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(); ....... }

Related


More Related Content