The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with composition. Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition.
Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses extends key word.
Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom. Composition simply means using instance variables that refer to other objects. The class House will have an instance variable, which refers to a Bathroom object.
Which one to use?
Inheritance should be only used when subclass ‘is a’ superclass.
Don’t use inheritance just to get code reuse. If there is no ‘is a’ relationship then use composition for code reuse. Overuse of implementation inheritance (uses the “extends” key word) can break all the subclasses, if the superclass is modified.
Do not use inheritance just to get polymorphism. If there is no ‘is a’ relationship and all you want is polymorphism then use interface inheritance with composition, which gives you code reuse.
Don’t use inheritance just to get code reuse. If there is no ‘is a’ relationship then use composition for code reuse. Overuse of implementation inheritance (uses the “extends” key word) can break all the subclasses, if the superclass is modified.
Do not use inheritance just to get polymorphism. If there is no ‘is a’ relationship and all you want is polymorphism then use interface inheritance with composition, which gives you code reuse.
Difference between dependency, association,aggregation and composition:
Though Java is a true object-oriented language that thoroughly supports inheritance, careful thought should be given to the use of this feature, since in many cases an alternative is to use a more flexible object relationship. The commonly identified object relationships are as follows:
a) dependency
b) association
c) aggregation
d) composition
b) association
c) aggregation
d) composition
The distinction between these relationships is based on the duration and the nature of the relationship.
The aggregation and composition relationships involve a tighter binding between the related objects. The related objects have a long-term relationship and have some level of mutual dependency, which may be exclusive, that defines their existence.
Dependency
An object dependency exists when there is a short-term relationship between the objects. For instance, the relationship between a shopping cart and a checkout object would be short term, since once the checkout operation is complete, the checkout object would no longer be needed. The same relationship would exist for a stock transfer object that needed to use a stock item object to get the information on the stock item being transferred. The stock transfer object would have a dependency relationship with the stock item object; the stock transfer object would read the transfer information and could then discard the stock item object and continue processing.
AssociationAn object association represents a more long-term association than the dependency relationship. The controlling object will obtain a reference to the association object and then use the reference to call methods on the object. The relationship between a car and a driver is representative of this relationship. The car will have a driver who will be associated with the car for a period of time.
Aggregation is an association in which one class belongs to a collection. This is a part of a whole
relationship where a part can exist without a whole. With an aggregation relationship, the contained object is part of the greater whole. There is a mutual dependency between the two objects, but the contained object can participate in other aggregate relationships and may exist independently of the whole. For example, a FileReader object that has been created using a File object represents a mutual dependency where the two objects combine to create a useful mechanism for reading characters from a file. The UML symbols for expressing this relationship as shown in following figure which involve a line connecting the two classes with a diamond at the object that represents the greater whole.
relationship where a part can exist without a whole. With an aggregation relationship, the contained object is part of the greater whole. There is a mutual dependency between the two objects, but the contained object can participate in other aggregate relationships and may exist independently of the whole. For example, a FileReader object that has been created using a File object represents a mutual dependency where the two objects combine to create a useful mechanism for reading characters from a file. The UML symbols for expressing this relationship as shown in following figure which involve a line connecting the two classes with a diamond at the object that represents the greater whole.
Composition is an association in which one class belongs to a collection. This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted. So composition has a stronger relationship.
With the composition relationship, the client object is owned by the greater whole. The contained object cannot participate in more than one compositional relationship. An example of this is a customer object and its related address object; the address object cannot exist without a customer object. This relationship is shown with a darkened diamond at the object, which represents the greater whole, as shown in the follwing figure
....will be updating with example code later.....
very first diagram on top shows incorrect relationship "of has" a which is by the way Composition.
ReplyDeleteThanks. Good one.
ReplyDeleteVery Useful. Thanks. Murugan (Chennai, India).
ReplyDeleteIt was very useful and it will be great if u upload java codes showing the implementation of different relationships
ReplyDeleteIt was very useful and it will be great if u upload java codes showing the implementation of different relationships
ReplyDeletethank you
ReplyDeleteAmiable dispatch and this mail helped me alot in my college assignement. Say thank you you on your information.
ReplyDelete