by BehindJava

Difference between Inheritance and Composition in Java and Object Oriented Programming

Home » java » 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.

Inheritance vs Composition

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.

rrr

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.