Whilst inheritance can be very good option on reusing the existing class and adding more functionalities to it, but using it as in every solution may not be a good idea.
For example, if there is a class Notepad
with operation of storage which can be file storage or cloud based storage, we can outsource this to another class instead of extending Notepad
class.
There are some problems with using inheritance as every possible solution. These are described below:
Subclass Explosion
When a class has multiple dimensions then there can be several subclasses as combinations of those dimensions. This is called subclass explosion.
For example, class Shape
can have dimension Color
and there can be different shapes. So, there can be
This issue can be solved by composition. We can use composition methods like Adapter method, Bridge method