+ 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?

24th Nov 2019, 2:03 PM
Herlon Costa
Herlon Costa - avatar
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.
24th Nov 2019, 3:34 PM
Logomonic Learning
Logomonic Learning - avatar
+ 2
Hum, i got it! Thank you so much!
24th Nov 2019, 3:39 PM
Herlon Costa
Herlon Costa - avatar