+ 2
Why not use inheritance?
I have read in some forums that it is not ideal to use the concept of object-oriented inheritance. Would you like to know why? What happens in code when using this concept?
2 Answers
+ 2
# Inheritance
- has an 'is a' relationship.
- e.g. a poodle is a dog is a mammal.
The problem with inheritance is that the child class includes everything from the parent class.
E.g. rubber duck will inherit eating and alive properties from its parent class duck which has a parent class of animal (alive=true).
Principle: "favour composition over inheritance"
# Composition
- has a "has a" relationship.
- allows you to customise objects without redundant code unlike inheritance.
+ 2
Hum, i got it! Thank you so much!