Aggregation in Java programming

Kayvan Kaseb
Software Development
3 min readFeb 25, 2020
Aggregation in Java Programming

If you want to study Java effectively, you will consider the concept of Aggregation as a key factor in learning Object-Oriented as well as Java. Therefore, considering this issue could be helpful for understanding and implementing Object-Oriented concepts. This essay will discuss Aggregation in Java as a problematic issue in order to enhance our skills in Java programming.

Prior to starting this term, we should have a look at Association term because Aggregation is a way to achieve Association. In fact, Association is relation between two separate classes, which establishes through their Objects. Association can be defined as follows: one-to-one, one-to-many, many-to-one, and many-to-many. In Object-Oriented programming, an object communicates to other objects for using functionality and services provided by that objects. In other words, each object has its own life-cycle and there is no owner. Basically, Composition and Aggregation are classified as the two forms of Association. Composition is a restricted form of Aggregation in which two entities are highly dependent or strong relationship on each other. For example, Human and Brain. A Human needs Brain to live, and a Brain needs a Human body to survive.

Aggregation is a special form of Association, which is defined as an unidirectional one way connection between classes or entities. For instance, Wallet and Money classes. Wallet has Money, but Money does not need to have Wallet necessarily. Thus, it is considered as one directional relationship. In short, we can be able to mention this term as below:

  1. Aggregation is a way to achieve Association.

2. If a class have an entity reference, it is known as Aggregation.

3. Aggregation in Java represents HAS-A relationship.

4. It represents a weak relationship between objects.

5. It illustrates the relationship in where one object contains other objects as a part of its state

In the below example, EmployeeClass has an object of AddressClass, and addressObject contains its own information such as city, state, country. In other words, EmployeeClass HAS-A AddressClass.

Implementing AddressClass
Implementing EmployeeClass (using aggregation)

However, the question is when we should use Aggregation or Inheritance (is-a relationship) in our programs because as we know Inheritance is the another concept in Object-Oriented programming that is associated with connection of objects.

  • If the new class is more or less as the original class, we can use inheritance. So, the new class is a subclass of the original class now.
  • If the new class must have the original class, we can use aggregation. The new class has the original class as a member now.

Advantage of using Aggregation in our programs:

Actually, the main benefit of using Aggregation is code re-usability. Re-usability is the use of existing assets or components in some forms within the software product during a development process. As a result, it means this issue can play an essential role in developing a software with high quality.

The major advantages for a software that use reuse concept are as below:

  • Increase software productivity.
  • Shorten software development time.
  • Develop software with fewer people.
  • Move components more easily from project to another one.
  • Reduce software development and maintenance costs.
  • Produce more standardized software.
  • Produce better quality software and provide a powerful competitive advantage.

In conclusion, Aggregation allows you to design classes that follow good Object Oriented practices. It also provide code re-usability; however, we should use other concepts of Object-Oriented programming wisely in various situations.

--

--

Kayvan Kaseb
Software Development

Senior Android Developer, Technical Writer, Researcher, Artist, Founder of PURE SOFTWARE YAZILIM LİMİTED ŞİRKETİ https://www.linkedin.com/in/kayvan-kaseb