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

Difference between Inheritance and Composition in Java and Object Oriented Programming

Though both Inheritance and Composition provide code reusability, the main difference between Composition and Inheritance in Java is that Composition allows reuse of code without extending it but for Inheritance, you must extend the class for any reuse of code or functionality. Another difference that comes from this fact is that by using Composition you can Reuse Code for even the final class which is not extensible but Inheritance cannot reuse code in such cases. Also by using Composition, you can reuse code from many classes as they are declared as just a member variable, but with Inheritance, you can reuse code from just one class because in Java you can only extend one class because multiple Inheritance is not supported in Java.


This post first appeared on Javarevisited: Blog About Java Programmin, please read the originial post: here

Share the post

Difference between Inheritance and Composition in Java and Object Oriented Programming

×

Subscribe to Javarevisited: Blog About Java Programmin

Get updates delivered right to your inbox!

Thank you for your subscription

×