Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java - What are the difference between composition and inheritance in Java ?

Composition - has-a Relationship between classes.
Inheritance - is-a relationship between classes.

Composition - Composing Object holds a reference to composing classes and hence relationship is loosely bound. 
Inheritance - Derived object carries the base class definition in itself and hence its tightly bound.

Composition - Used in Dependency Injection
Inheritance - Used in Runtime Polymorphism

Composition - Single class objects can be composed within multiple classes.
Inheritance - Single class can only inherit 1 Class.

Composition - Its the relationship between objects.
Inheritance - Its the relationship between classes.





This post first appeared on Techsharepoint, please read the originial post: here

Share the post

Java - What are the difference between composition and inheritance in Java ?

×

Subscribe to Techsharepoint

Get updates delivered right to your inbox!

Thank you for your subscription

×