310 likes | 677 Views
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.
E N D
Introduction • In normal terms, they both refer to member object but the survival or existence of the member object makes the differences.
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.
Aggregation Continues • Example:Room has a table and the table can exist without the room. The table can have meaning without the room also.
How to implement Aggregation in coding • How can we manage that?
Implement Aggregation in coding • public class Address{ . . .} • public class Person • { • private Address address; • public Person(Address address) • { • this.address = address; • } • . . . • }
Implement Aggregation in coding • Address address = new Address();Person person = new Person(address); • or • Person person = new Person( new Address() );
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.
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.
How to implement Composition in coding • How can we manage that?
Implement Composition in coding • public class Engine{ . . . } • public class Car • { • Engine e = new Engine(); • ....... • }