Wednesday, 12 July 2017

Difference between Inheritance and Composition in Java OOPS

Though both Inheritance and Composition provides code reusablility, 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 which comes from this fact is that by using Composition you can reuse code for even 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 form just one class because in Java you can only extend one class, because multiple Inheritance is not supported in Java. You can do this in C++ though because there one class can extend more than one class. BTW, You should always prefer Composition over Inheritance in Java, its not just me but even Joshua Bloch has suggested in his book Effective Java, which is a great resource to learn how to do things in right way in Java.

Inheritance vs Composition

Now let's understand difference between Inheritance and Composition in little bit more detail. I will go point by point and try to explain each point in as much detail as possible without boring you :)

1) Static vs Dynamic

First difference between Inheritance and Composition comes from flexibility point of view. When you use Inheritance, you have to define which class you are extending in code, it cannot be changed at runtime, but with Composition you just define a Type which you want to use, which can hold its different implementation. In this sense, Composition is much more flexible than Inheritance.

2) Limited code reuse with Inheritance

As I told, with Inheritance you can only extend one class, which means you code can only reuse just one class, not more than one. If you want to leverage functionalities from multiple class, you must use Composition. For example, if your code needs authentication functionality, you can use an Authenticater, for authorization you can use an Authorizer etc, but with Inheritance you just stuck with only class, Why? because Java doesn't support multiple Inheritance. This difference between Inheritance vs Composition actually highlight a severe limitation of later.

3) Unit Testing

This is in my opinion most important difference between Inheritance and Composition in OOP and probably is the deciding factor in whether to use Composition or Inheritance. When you design classes using Composition they are easier to test because you can supply mock implementation of the classes you are using but when you design your class using Inheritance, you must need parent class in order to test child class. Their is no way you can provide mock implementation of parent class.

4) Final classes

Third difference between them also highlight another limitation of Inheritance. Composition allows code reuse even from final classes, which is not possible using Inheritance because you cannot extend final class in Java, which is necessary for Inheritance to reuse code.

5) Encapsulation

Last difference between Composition and Inheritance in Java in this list comes from Encapsulation and robustness point of view. Though both Inheritance and Composition allows code reuse, Inheritance breaks encapsulation because in case of Inheritance, sub class is dependent upon super class behavior. If parent classes changes its behavior than child class is also get affected. If classes are not properly documented and child class has not used the super class in a way it should be used, any change in super class can break functionality in sub class.

Oracle Java Inheritance, Java OOPS

That's all about difference between Inheritance and Composition in Java and OOP. You can see that even though Inheritance and Composition has same goal to assist in reusing tried and tested goal their choice brings different challenges. Composition provides better way to reuse code and same time protect the class you are reusing from any of its client, but Inheritance doesn't offer that guarantee. Sometime though Inheritance is necessary, mainly when you are creating class from same family.

Related Posts