Association, Aggregation, And Composition in Java-1
Association, Aggregation, And Composition in Java-1
(LALPUR)
Seminar Assignment
2
What is Association?
• Association defines a general connection between two classes
where one class uses or interacts with another class. It implies
a 'uses-a' relationship, allowing instances of one class to hold
references to instances of another class without a strong
dependency. Association is bi-directional or unidirectional.
Association
3
Types of Association
• 1. One-to-One: Each object of one class is associated with a
single object of another class.
• 2. One-to-Many: An object of one class is associated with
multiple objects of another class.
• 3. Many-to-One: Many objects of one class are associated with
a single object of another class.
• 4. Many-to-Many: Multiple objects of one class are associated
with multiple objects of another class.
4
Example of Association in Java
Example:
The Teacher class and Subject class are separate, and each can exist independently.
Code:
• // Class representing a Teacher
• class Teacher {
• String name;
6
What is Aggregation?
• Aggregation is a form of Association, often described as a 'has-
a' relationship. It represents a whole-part relationship where
the part can exist independently of the whole. This means the
lifecycle of the part is not dependent on the whole.
Association
Aggregation
7
Aggregation Characteristics
• 1. The parts are not dependent on the whole object’s lifecycle.
• 2. Aggregation allows a class to reuse other classes' objects
and functionalities without needing ownership over the
objects.
• 3. Represented by a hollow diamond in UML diagrams.
8
Example of Aggregation in Java
• Example:
• A person and an Address have an aggregation relationship, as Address can exist independently of the Person.
• Code:
• // Class representing an Address
• class Address {
• String city, state, country;
• public Address(String city, String state, String country) {
• this.city = city;
• this.state = state;
• this.country = country;
• }
• public String getFullAddress() {
• return city + ", " + state + ", " + country;
• }
• }
• //Person that "has-a" Address
• class Person {
• String name;
• Address address; // Aggregation relationship
• public Person(String name, Address address) {
• this.name = name;
• this.address = address;
• }
• public void displayInfo() {
• System.out.println("Name: " + name);
• System.out.println("Address: " + address.getFullAddress());
• }
• }
• class Main {
•
•
public static void main(String[] args) { 9
Address address = new Address("New York", "NY", "USA");
• // Creating a Person object with the Address object
• Person person = new Person("John", address);
person.displayInfo();
• }
When to use Aggregation?
Code reuse is also best achieved by aggregation when there is no
is-a relationship.
• Inheritance should be used only if the relationship is-a is
maintained throughout the lifetime of the objects involved;
otherwise, aggregation is the best choice.
10
What is Composition?
• Composition is a strong form of Aggregation where the parts
are strongly dependent on the whole. It is a 'part-of'
relationship. In Composition, if the whole is destroyed, the
parts are destroyed too, as they do not exist independently.
Association
Aggregation
Composition
11
Composition Characteristics
• 1. Strong lifecycle dependency between the container and its
parts.
• 2. The parts cannot exist independently without the whole.
• 3. Represented by a filled diamond in UML diagrams.
12
Example of Composition in Java
• A computer contains a processor, and the processor cannot function independently outside of the
computer. If the computer is destroyed, the processor’s existence is also terminated. Code:
14
References and Research
15
• Thank You.
16