Saturday, June 25, 2022

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.


You can do this in C++ though because there one class that can extend more than one class. BTW, You should always prefer Composition over Inheritance in Java, it 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 the right way in Java. I have listed my argument in favor of Composition over Inheritance in my earlier post, which you can check now or later.





Inheritance vs Composition? When to use? 

Now let's understand the difference between Inheritance and Composition in a 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 Behavior

The first difference between Inheritance and Composition comes from a 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 you, with Inheritance you can only extend one class, which means your code can only reuse just one class, not more than one. If you want to leverage functionalities from multiple classes, you must use Composition. 

For example, if your code needs authentication functionality, you can use an Authenticator, 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 highlights 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 a 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. There is no way you can provide a mock implementation of the parent class.


4. Final classes

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



5. Encapsulation

The last difference between Composition and Inheritance in Java in this list comes from the Encapsulation and robustness point of view. Though both Inheritance and Composition allow code reuse, Inheritance breaks encapsulation because, in the case of Inheritance, sub-class is dependent upon super class behavior. If parent classes change its behavior then child class is also get affected.

If classes are not properly documented and the child class has not used the superclass in a way it should be used, any change in super class can break functionality in sub class. In order to understand with a great example, I strongly suggest you read Effective Java Item 16 and 17.

Difference between Inheritance and Composition in Java


That's all about the difference between Inheritance and Composition in Java and OOP. You can see that even though Inheritance and Composition have the same goal to assist in reusing tried and tested goals their choice brings different challenges. 

The composition provides a better way to reuse code and same time protect the class you are reusing from any of its clients, but Inheritance doesn't offer that guarantee. Sometimes though Inheritance is necessary, mainly when you are creating a class from the same family.

Other Object Oriented Programming Articles you may like
P. S. - If you have any questions bout Inheritance and Composition or pros and cons of each of them, feel free to ask in comments. If you need free resources to learn OOP and Java then you can also checkout these free object oriented programming courses for beginners. 

12 comments:

  1. I have also read Effective Java and from that I can say that Composition is better than Inheritance but Inheritance is equally good for defining type hierarchy, so you cannot undermine importance of Inheritance.

    ReplyDelete
  2. House is a Building. The UML diagram is incorrect

    ReplyDelete
  3. Awesome article.

    ReplyDelete
  4. Excellent help on understanding. Thanks

    ReplyDelete
  5. Thank you @Dinesh, glad that you like this article. if you have any question/doubt, feel free to ask.

    ReplyDelete
  6. The UML diagram is incorrect for composition relationship and inheritance.

    ReplyDelete
  7. wow please correct this, the diagram is wrong

    ReplyDelete
  8. This is a better example of UML diagram:
    Inheritance = A cat is an animal. Composition = A car has an engine.

    ReplyDelete
  9. Great article. I find every point in your articles worth noting. I have never written unit tests myself. I have a little knowledge about it. So I have a doubt here: why can't we mock a parent class? if class B extends A why can't we create a mock of A like we mock any class using Mockito?

    ReplyDelete
  10. You can definitely mock using Mockito.

    ReplyDelete