Aggregation is a whole/part relationship. If whole does not exist no longer, but part will still exist
But in composition If whole does not exist no longer, but part will no longer exist
For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors.
My question here how we will actually define the class definition of University,Department and Professors in java which also depicts above aggregation and composition behavior?
The concepts of composition and aggregation are displayed with arrows with diamonds in UML, but when those concepts are implemented in a programming language, may change from one programing language to another.
Generally speaking, in programming languages, like Java, C# or Delphi, both cases are represented by object references. Each object has a "life cycle" ("created", "do stuff", "destroyed").
package Universities;
class ProfessorClass {
string FirstName;
string LastName;
// constructor
public ProfessorClass(string AFirstName, string ALastName) {
FirstName = AFirstName;
LastName = ALastName;
}
} // class ProfessorClass
class DepartmentClass {
string Name;
string Description;
// constructor
public DepartmentClass(string AName, string ADescription) {
Name = AName;
Description = ADescription;
}
} // class DepartmentClass
class UniversityClass {
// here doesn't look different:
// aggregation
ProfessorClass[] Professors;
// composition
DepartmentsClass[] Departments;
// constructor
public UniversityClass() {
// "UniversityClass" is in charge of creating parts
// here doesn't look different:
DepartmentsClass = new DepartmentsClass[]();
ProfessorClass = new ProfessorClass[]();
}
public addDepartment(string AName, string ADescription)
{
// composition, whole class is in charge of adding parts:
DepartmentClass Dept = new DepartmentClass(AName, ADescription);
DepartmentsClass.add(Dept);
}
public deleteDepartment(string AName)
{
// composition, whole class is in charge of deleting parts:
DepartmentsClass.delete(AName);
}
public addProfessor(ProfessorClass AProfessor)
{
// aggregation, whole class only reference other objects,
// but, may look like they where parts
ProfessorClass.add(AProfessor);
}
public static void main(String[] args) {
UniversityClass MyUniversity = new UniversityClass();
// composition, internal, maintained by main class
MyUniversity.addDepartment("History", "Very Boring");
MyUniversity.addDepartment("Music", "Only if you like it");
MyUniversity.addDepartment("Astronomy", "night living people");
// aggregation, external, referenced by main class,
// maintained independently
ProfessorClass Professor1 = new ProfessorClass("Jane", "Doe");
ProfessorClass Professor2 = new ProfessorClass("Mike", "Smith");
ProfessorClass Professor3 = new ProfessorClass("Louise", "Kent");
MyUniversity.addProfessor(Professor1);
MyUniversity.addProfessor(Professor2);
MyUniversity.addProfessor(Professor3);
} // static void main(...)
} // class UniversityClass
In composition, "whole" objects that are composed by other objects ("parts"), are in charge of creating, using & destroying its parts. The "whole" object its responsible for the "life cycle" of each of its "parts".
In aggregation, "whole" objects that reference other objects ("aggregation"), that may look similar to "parts" (composition), usually, are not created or destroyed by the main object, just assigned or deassigned. The main object doesn't control directly the other objects, just add or remove references.
There are some times you may pick code alredy done from other developers, and may see both ideas, mixup, and cannot difference which concept is been used.
Let's set the terms. The Aggregation is a metaterm in the UML standard, and means BOTH composition and shared aggregation, simply named shared. Too often it is named incorrectly "aggregation". It is BAD, for composition is an aggregation, too. As I understand, you mean "shared".
Further from UML standard:
composite - Indicates that the property is aggregated compositely,
i.e., the composite object has responsibility for the existence and
storage of the composed objects (parts).
So, University to cathedras association is a composition, because cathedra doesn't exist out of University (IMHO)
Precise semantics of shared aggregation varies by application area and
modeler.
I.e., all other associations can be drawn as shared aggregations, if you are only following to some principles of yours or of somebody else. Also look here.