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?
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:
So, University to cathedras association is a composition, because cathedra doesn't exist out of University (IMHO)
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.
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").
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.