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
Why prefer Composition over Inheritance?
- Classes should use composition instead of inheritance from a parent class to enable polymorphic behavior and code reuse. According to the design concept, the composition should be preferred over inheritance to get a better level of design flexibility.
- One justification for choosing composition in Java is the lack of support for multiple inheritances. Since Java only allows for the extension of a single class, Reader and Writer functionality is required if you require multiple features, such as the ability to read and write character data into files. Having them as private members simplifies your task, and this is what is meant by composition.
- Flexibility is another factor that makes composition superior to inheritance. If you use composition, you have the flexibility to swap out an outdated and improved Composed class implementation. Utilizing the comparator class, which offers features for comparison, is one example.
- The ability to reuse code is provided by both composition and inheritance. However, inheritance has the drawback of breaking encapsulation. If the subclass’s function depends on the superclass’s action, it suddenly becomes fragile. Sub-class functionality may become unusable when the behavior of the super-class changes without any change on the part of the sub-class.
- Multiple object-oriented design patterns from Gang of Four: Elements of Reusable Object-Oriented Software, listed in the timeless classic Design Patterns, favor composition over inheritance. The strategy design pattern serves as the finest example of this, using composition and delegation to alter the Context’s behavior without altering the context code. Because Context relies on composition to carry strategy instead of inheriting it, it is easy to have a new implementation of strategy at run-time.
Composition In Java Example
The types of association used to depict the link between two classes are aggregation and composition in java. In the case of composition, If an existing university object is destroyed, all of the department objects will also be destroyed automatically because there is no way for dependent objects to exist without an existing university object. This strong association between the objects is what is meant by the term composition.
How Does Composition Work in Java?
Since the PART-OF type of relation between two entities is implemented using composition, one of the entities is referred to as a container and the other as a composed entity. The ability to utilize this class as a composite entity in other container classes encourages code reuse. A composition in Java example can include a container class for an engine being a car, a two-wheeler, etc.
Both have dependencies on one another since the combined class is a component of a container entity. However, a composed class can still be void; for example, a car need not be required to have an engine. As a result, the container class serves as the sole source of existence for the composed class. PART-OF relation is also referred to as a subclass of HAS-A relation because Composition is a type of association. Composition assists in establishing a relationship between two entities that are interdependent without the use of inheritance in this way.
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.
|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.
|It can be easily accomplished at the runtime.
|The features are better accomplished at compile time.
|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.
|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:
|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.