0% found this document useful (0 votes)
1 views

Association, Aggregation, And Composition in Java-1

The document is a seminar assignment by Rishi Raj Choudhary from Birla Institute of Technology, focusing on object relationships in Java, specifically Association, Aggregation, and Composition. It explains the definitions, characteristics, and examples of each relationship type, emphasizing their importance in structuring robust and reusable code. Additionally, it provides guidance on when to use each relationship type in programming.

Uploaded by

rcrishi19
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Association, Aggregation, And Composition in Java-1

The document is a seminar assignment by Rishi Raj Choudhary from Birla Institute of Technology, focusing on object relationships in Java, specifically Association, Aggregation, and Composition. It explains the definitions, characteristics, and examples of each relationship type, emphasizing their importance in structuring robust and reusable code. Additionally, it provides guidance on when to use each relationship type in programming.

Uploaded by

rcrishi19
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

BIRLA INSTITUTE OF TECHNOLOGY,MESRA

(LALPUR)

Seminar Assignment

Association, Aggregation, and


Composition in Java
Understanding Object Relationships in Java

NAME : Rishi Raj Choudhary


Roll No. : MCA/40018/24
Department: MCA
Table of Contents
• 1. Content
• 2. Introduction to Object Relationships
• 3. What is Association?
• 4. Types of Association
• 5. Example of Association in Java
• 6. When to Use Association
• 7. What is Aggregation?
• 8. Aggregation Characteristics
• 9. Example of Aggregation in Java
• 10. When to use Aggregation?
• 11. What is Composition?
• 12. Composition Characteristics
• 13. Example of Composition in Java
• 14. When to use Composition? 1
• 15. References and Research
• 16. End
Introduction to Object Relationships

• In Java, objects interact with each other through various types


of relationships. These relationships help in structuring a
robust and reusable code base by defining how different
classes connect. The main relationships in Java are
Association, Aggregation, and Composition.

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;

• public Teacher(String name) {


• this.name = name;
• }
• public String getName() {
• return name;
• }
• }
• // Class representing a Subject
• class Subject {
• String subjectName;

• public Subject(String subjectName) {


• this.subjectName = subjectName;
• }

• public String getSubjectName() {


• return subjectName;
• }
• }
• // Main class to test Association
• public class Main {
• public static void main(String[] args) {


// Creating objects of Teacher and Subject
Teacher teacher = new Teacher("Mr. Smith");
5
• Subject subject = new Subject("Mathematics");

• // Displaying the association


• System.out.println(teacher.getName() + " teaches " + subject.getSubjectName());
• }
When to Use Association
• One class needs to interact with or use the functionality
of another class..
• User need to represent a many-to-many relationship

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:

// Class representing Processor


class Processor {
String model;
public Processor(String model) {
this.model = model;
}
public String getModel() {
return model;
}
}
// Class representing Computer that "has-a" Processor
class Computer {
String brand;
Processor processor;

public Computer(String brand, String processorModel) {


this.brand = brand;
this.processor = new Processor(processorModel); // Processor created as part of the Computer
}
public void displayInfo() {
System.out.println("Computer Brand: " + brand);
System.out.println("Processor Model: " + processor.getModel());
}
}

// Main class to test Composition


public class Main { 13
public static void main(String[] args) {
Computer computer = new Computer("Dell", "Intel Core i7");
computer.displayInfo();

// When 'computer' is destroyed, 'processor' is also destroyed as it is a part of the computer.


}
When to use Composition?

• Has-a Relationship: Use composition when one class logically "has-a"


relationship with another. For example, a computer "has" processor or a car
"has" an engine.
• Encapsulation:Composition allows for better encapsulation by hiding the details
of the composed object from the outside world, offering greater control over the
internals of the system.

14
References and Research

• Association, Composition and Aggregation in Java -


GeeksforGeeks
• Association, Composition and Aggregation in Java - Scaler
Topics
• ChatGPT
• Difference Between Aggregation and Composition in Java -
Java point

15
• Thank You.

16

You might also like