Basics of Association in Java:
Association can be defined in Java as an interconnection between two individual classes using their distinct objects. The types of relationships managed by Java association are:
- One-to-one: There exists only a single derived class for each parent class.
- One-to-many: A single parent class may have more than one child class.
- Many-to-one: A single child class may be associated with more than one superclass.
- Many-to-many: A number of parent classes may be associated with a single child class and more number of child classes may be associated with a single parent class.
Once the relationship is established, the derived classes communicate with the respective base classes to reuse their characteristics and properties. There are two types of association in Java. They are aggregation and composition.
Aggregation in Java is a more generalized process as compared to the composition. These forms are based on the type of relationship supported between the classes.
Introduction to Composition in Java:
A designing technique in Java that implements a Has-A relationship is referred to as composition. The process of inheritance is used to reuse the code. The composition in Java can be accomplished using an instance variable referring to other objects. If an object constitutes another object in such a way that the constituent object cannot exist without the survival of the main object, the kind of relationship is referred to as the composition. To be more specific, the composition is an aspect that describes the reference among two or more classes with the help of instance variables. Here, the instance should be created before using the instant variables. Let us take an example of a Library for a clear understanding of the concept ‘composition’.
There exist numerous books in a library. Each book has separate authors and titles. The library should also have a reference list of books in it. The library also has several books related to the same subject or diverse subjects. Here, the library corresponds to the main class and the books can be related to the derived class. The association or the relationship between the books and the library can be regarded as composition. This is because the class “Books” is completely dependent on the class library. For instance, if the library is destroyed, then all the books in it are also destroyed.
Check out our free technology courses to get an edge over the competition.
Explore our Popular Software Engineering Courses
A detailed description of Composition in Java:
The Composition is a form of Java association. The two communicating classes are strongly interlinked, where the derived class is completely dependent on the parent class. The independent existence of the derived class is not possible in this case. For example, an engine without a car cannot exist independently. This association type is highly restricted as compared to the aggregation. Composition is a design technique and should not be confused with a Java feature.
The Composition can be used to model objects that have other objects as their constituent members. There exists the has-a relationship between these objects. In this association type, one object contains the other object. Hence, the constituent object is completely dependent on the main object for its survival. So, if the containing object is ruined, the constituent object is also affected. Therefore, the composition relationship can be viewed as a part of the whole relationship in which the existence of the part without the whole is not possible. In other words, the part is automatically deleted on the deletion of the whole. This implies that the whole has a firm relationship with the part.
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
In-Demand Software Development Skills
How is inheritance different from composition in Java?
It is important to note that the functionality of inheritance can be accomplished using composition in Java as well. Though both inheritance and composition are used to offer reusability of the code by the relating class, there are subtle differences between these two. The main difference between the two processes is that composition is a technique of design, unlike inheritance which is a Java feature. Other major differences between the two are listed in the table below.
Parameter |
Composition |
Inheritance |
Basic | It incorporates a HAS-A relationship. | It incorporates an IS-A relationship. |
Reuse of code | The code can be reused in multiple classes. | The reuse of code is possible only in one class because only one interface can be extended by a class. |
Scope | It can be easily accomplished at the runtime. | The features are better accomplished at compile time. |
Final | It facilitates the reuse of code even from the final classes. | The code from the final classes cannot be reused in the case of inheritance. |
Methods | The methods are not exposed. Public interfaces are used for their interaction. | Inheritance exposes both protected and public methods of the base class. |
Differences between Aggregation and Composition:
Aggregation |
Composition |
It is a type of weak Java association. | It is a stronger association type when compared to aggregation. |
The derived class is independent of the base class. Hence, the derived class can continue to exist even if the base class is destroyed. | The derived class is completely dependent on the base class. Therefore, the subclass cannot exist if the superclass is destroyed. |
The child class has its own lifetime. | The lifetime of the child class is dependent on the parent class. |
The parent class uses the child class in this type of association. Hence, the derived class is not owned by the base class. | In this association type, the parent class owns the child class. Hence, the parent class is the owner of the child class. |
It constitutes a HAS-A relationship. The derived class has a base class. | It constitutes a part-of relationship. The derived class is a part of the base class. |
Final keyword is not used to denote aggregation. | Final keyword may be used to denote composition. |
Example: The car has a driver. | Example: Engine is a part of the car. |
Read our Popular Articles related to Software Development
Advantages of using Composition in Java:
- Composition facilitates the reuse of the Java code.
- Though multiple inheritances are not supported by Java, the gap can be filled in by this design technique.
- With the use of composition, a class can be tested for ability in a better manner.
- The code is made more flexible with the use of composition. The composed class implementation can be replaced with an enhanced version.
- The object members can be changed at runtime for the dynamic change of the program’s behavior with the use of a composition relationship.
Limitations of composition in Java:
There are a few drawbacks of using composition in Java. The main disadvantage of the object composition is that it is harder for the user to understand the behavior of the implemented system just by pondering over the source code. A system that uses object composition is highly dynamic in nature. So, understanding the functioning of the systems that use object composition requires a deeper analysis of the source code and running the code to witness the functioning and cooperation between the individual objects in the source code.
If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s Executive PG Programme in Software Development – Specialisation in Full Stack Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.