java class reusability tutorial

java class reusability tutorial which gives you a detailed Explanation on Composition, Aggregation of            Inheritance.

Learn in detail about the advantages and disadvantages of Composition and Aggregation.each of this explained along with the program to make you better understand the concept and implantation. 

 

java class reusability tutorial

Class reusability:

Reusability means being able to create a new class that uses the features of an existing class without recording those features. There are actually several ways to achieve reusability in object-oriented programming.

Composition:

The composition is restricted form of Aggregation.

A composition is the design technique to implement has-a relationship in classes.

Java composition is achieved by using instance variables that refer to other objects. For example, an Employee has a Job (or) a Car has an Engine.

For example, a building has rooms. A room can exist only as part of a building. The room cannot be removed from one building and attached to a different one. When the building ceases to exist so do all rooms that are part of it

Contained objects are accessed by the containing class solely through their interfaces

Advantages :

Contained objects are accessed by the containing class solely through their interfaces

“Black-box” reuse, since internal details of contained objects, are not visible

Good encapsulation

Fewer implementation dependencies

Each class is focused on just one task

The composition can be defined dynamically at run-time through objects

acquiring references to other objects of the same type

till here you have understood the composition and its advantages and disadvantages.

Aggregation :

In aggregation, the part may have an independent life cycle, it can exist independently. When the whole is destroyed the part may continue to exist.

For example, a car has many parts. A part can be removed from one car and installed into a different car. If we consider a salvage business, before a car is destroyed, they remove all saleable parts. Those parts will continue to exist after the car is destroyed.

Advantages:

Simplicity: Client can deal directly with the containing object. Instead of dealing with individual pieces.

Safety: sub-objects are encapsulated.

Specialized Interface:-

general objects may be used together with an interface that is specialized to the problem at hand.

Both aggregation and composition represent a whole-part (has-a/part-of) Association.

The main differentiation between aggregation and composition is the life cycle dependence between whole and part.

In aggregation, the part may have an independent life cycle, it can exist independently. When the whole is destroyed the part may continue to exist.

The composition is a stronger form of aggregation. The life cycle of the part is strongly dependent on the life cycle of the whole. When the whole is destroyed, the part is destroyed too.