To Object-Oriented Programming: COMP1161
To Object-Oriented Programming: COMP1161
Introduction
to
Object-oriented Programming
Aggregation
(Based mainly on Lewis and Loftus, Java Software Solutions, Ch. 4)
• One object communicates with another object by invoking the other object’s
methods
• In order to do this the invoking object must have a reference to the invoked
object
• The easiest way to do this is by making the invoked object an attribute of the
invoking object.
A Java Example:
public class Person {
private Name name;
private Car familyCar;
…
}
• The Person object can communicate with any of the two objects
A Java Example:
public class Person {
..
familyCar.setMileage(2000);
name.changeLastName(“James”);
}
An object of the class at the diamond end of the line holds a reference to an object
of the class at the other end of the line.
Copyright © 2012 Pearson Education, Inc.
Strong Aggregation
If the Person object is destroyed than the Name object is also destroyed.
Copyright © 2012 Pearson Education, Inc.
Weak Aggregation
The Car object can exist even if the Person object is destroyed.
Copyright © 2012 Pearson Education, Inc.
Uses of Aggregation
MainMenu AddressBook
DataBase