You are tasked with building an application that deals with cars. You are off designing the classes for cars unaware of the looming dangers of inheritance.
Every car has a drive functionality, the driver floors the accelerator and the car moves.
So, you design a base class called Car which has a function drive().
 method in this base class so that all the sub-car classes inheriting from this base Car class get it as it is common functionality. You feel very good about yourself for writing reusable code.
The client wants a car that can drive with petrol and another on electricity.
You create two classes for these:
.
.
 and Charge().
 method will now have to be duplicated across all the car classes except Toy Car class. You can see how we are now heading down a slippery slope right?
What we are seeing is a pattern of IS-A relations; and electric car IS A car. This is the root of the problem here. We should get rid of so many IS-A relations and compose a car object with HAS-A relations. HAS-A relations can be interpreted as a car HAS A drive behaviour. We can create separate interfaces for each behaviour:



