[go: up one dir, main page]

0% found this document useful (0 votes)
53 views2 pages

Software Design and Analysis: Assignment 1 Question 5

This document discusses the differences between association, aggregation, and composition in object-oriented design. Aggregation refers to a "has-a" relationship where two classes are linked but can exist independently. Composition is a "part-of" relationship where the contained class cannot exist without the container class. In Java code, aggregation involves passing an existing object to another class, while composition involves instantiating the contained class within the container class so destroying the container also destroys the contained parts.

Uploaded by

Faiq Rauf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views2 pages

Software Design and Analysis: Assignment 1 Question 5

This document discusses the differences between association, aggregation, and composition in object-oriented design. Aggregation refers to a "has-a" relationship where two classes are linked but can exist independently. Composition is a "part-of" relationship where the contained class cannot exist without the container class. In Java code, aggregation involves passing an existing object to another class, while composition involves instantiating the contained class within the container class so destroying the container also destroys the contained parts.

Uploaded by

Faiq Rauf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Software Design and Analysis

Assignment 1 Question 5

Name: Muhammad Daud Mazhar Roll No: 18L-0919 Section: BCS 5B

Association, Aggregation and Composition:


Association is simply a relationship that is established between different types of classes through
their objects. Association is obtained by using aggregation and/or composition.
Aggregation:
In aggregation, two different types of classes are linked together in a ‘has-a’ relationship. For
example, if we take Hospital and Doctors as different classes, then a Hospital would have a list
of objects of class ‘Doctors’. Importantly, we should note that Hospitals and Doctors can exist on
their own, which means that destruction of either object does not affect the other. Also, the
relationship is unidirectional, i.e. Doctors cannot have Hospitals, rather, only Hospitals can have
Doctors.
Implementation:
To implement this, we can simply give a reference to List of (already existing) Doctors class
inside the Hospital class. The constructed Doctors List can be passed onto Hospital’s overloaded
constructor as an argument.

Composition:
Composition is a ‘part-of’ relationship, i.e. the part contained cannot exist without its container.
Considering the same example as above and an additional class for Wards, we can safely assume
that an object of Wards Class cannot exist on its own unless it is contained in a Hospital Class.
Also, the Hospital cannot exist if it does not have any Wards. Therefore, this relationship is
composition.
Implementation:
We make the Wards Objects inside the Hospital Constructor since Hospital owns Wards.
Destruction of Hospital would automatically destroy all the Wards in it too.
Java Code Differences:
In Java, the code for Association in nothing very specific. It is simply using two different objects
(usually in the Main class) and establishing a relationship between them. Aggregation and
composition codes do differ in Java. For example, in Aggregation, we instantiate the two kinds
of objects separately, e.g. Doctors List would be created separately and then this list would be
passed onto the Hospital’s constructor as an argument. Even if the Hospital that has this Doctors
List, is destroyed, the Doctors would still exist separately. On the other hand, in composition, the
Wards objects will be created inside the Hospital Class, i.e. Wards would be a part of the
Hospital Class. Destroying a single Ward would not destroy the Hospital, although destroying
the Hospital would destroy all the Wards as they were instantiated inside Hospital.

You might also like