"motorspeed: 10" "motorspeed: 5" "motorspeed: 0" "model: Maxpro" "model: PowerMax"
As it can be seen the above code is much cleaner than the one below because here the machine functionality & Features are all separated. So the class can implement functionality suitable as per their requirements.Treadmill1 can implement the functionality of Treadmill when needed. Now, Treadmill1 can increase its motorspeed, decrease its motorspped, and even change its model name as per requirement. When talked about composition, the Inheritance gets assisted automatically.
"Inclinedspeed is: 10" "Declinedspeed is: 5" "Now the speed is: 0" "Maxpro" "PowerMax"
|Follows has-a relationship||Follows is-a relationship|
|Composition allows code-reuse.||Inheritance does not allow code-reuse.|
|In composition, you will no need to extend classes||In inheritance, you will need to extend classes.|
|In composition, you will no need to Mixin.||In inheritance, Mixin plays a major role|
|The composition is more flexible.||Inheritance is less flexible compared to composition.|
Conclusion: Both Inheritance & Composition yields the same result but the way of implementation matters. The composition implementation way is much easily readable & preferable to first inheritance.