Introduction
A lot of articles , books and software engineers advising that it's better to use composition instead of inheritance so why that, let's see.
What is Inheritance ?
Inheritance is a one of OOP (Object Oriented Programming) principles that provides to you a facility of reusing , expanding , sharing and building hierarchy of objects , which can make development process for some problems easier , Some programming languages allow multiple inheritance (multiple super class) like C++ and some don't , like Java.
By using inheritance you can remove duplicate code and apply polymorphism on your logic to provide extensible and maintainable logic, Till now that's awesome.
But how can inheritance harms you badly ?
The problem with inheritance is it's providing you with static base state which preventing you from dealing with different behaviors in same kind of type with flexibility, let's take an example.
We have here a super class called Bird that gather some properties and behaviors common between birds like fly() and eat(), also we have 2 sub classes OWL and PARROT, They inherit their properties and behaviors from their super class Bird, so good till now.
Problem appears here, let's say we want to add new type of birds like Penguin or Ostrich, and off course these are birds as well, so you make them inherit from Bird class, but you find that there is a fly() logic here and it's not applied to these kind of birds which neither Penguin nor Ostrich can fly, so for first look you can override these methods with empty logic and all good, whenever add new kind of birds override their logic to adapt them.
After a while you figured out that you have to implement migrate() logic to most of your subclasses, so you got better idea make this logic in super class, while implanting that you found that you have one type that doesn't migrate so you have to handle this special case also with same above solution.
Now you end with a lot of special cases with multiple handlers, And code now have smells, So you came up with solution that you will divide these relations based on behavioral kind like this.
Yeah it can work with this for a while but with growing of requirements you will find that the hierarchy has became more complex, your logic also became complex, start dealing with multiple If statements in code and your solution is losing his fancy style.
Composition to the rescue
while inheritance can be described as is-a relation like a Canary is a bird, composition can be described as has-a relation like a Canary has a flying behavior, so instead of building hierarchy of classes, your classes will be like this.
Now with composition you have a better solution with less complex class hierarchy, but best for last it's also provided you with interchangeable algorithms that you can assign and change in runtime with 100% of flexibility, also with some Factory classes you can change the behavior of logic at runtime easily which impossible to done this with inheritance.
Is that means Inheritance is bad?
No, not like that but you have to be careful when using it and also make sure 100% that class hierarchy you build is identical and no special cases, If there any special cases or you are not sure about what you do it's better to use Composition over Inheritance.
Resources
- Object Oriented Thought Process - Chapter 7 Mastering Inheritance and Composition.
- Head First Design Pattern - Chapter 1 Strategy Design Pattern.
IF YOU LIKED THE POST, THEN YOU CAN BUY ME A COFFEE, THANKS IN ADVANCE.
Top comments (2)
great article!!
Thanks🙏