Suresh Rohan's Blog

This blog is all about Java, J2EE,Spring, Angular, React JS, NoSQL, Microservices, DevOps, BigData, Tutorials, Tips, Best practice, Interview questions, Views, News, Articles, Techniques, Code Samples, Reference Application and much more

Monday, October 26, 2015

Object Composition In Java

The composite object shares has-a relationships with the containing objects, and the underlying concept is referred to as object composition.


Composition vs. Inheritance

Class inheritance implies an is-a relationship, interface inheritance implies an is-like-a relationship, and composition implies a has-a relationship.

Always check whether the is-a relationship exists between the derived classes and the base class. If the is-a relationship does not hold, it’s better to use composition instead of inheritance.



No comments:

Post a Comment