+ 1

What is inheritance exactly?

11th Apr 2017, 2:26 AM
Green
3 Answers
+ 1
inheritance is the concept that when a class is defined, any defined subclass of that class can inherit or get the members of that class. The members can be attributes or methods of that class.
11th Apr 2017, 3:33 AM
Deddy Tandean
+ 1
Let's say you have a game with a bunch of different enemies. Now, let's say you have the enemies Warrior and Wolf. All enemies should have Attack and Defense attributes, so you put those attributes in the class Enemy. Warrior and Wolf are enemies, so they inherit all attributes from class Enemy. However, Warriors can use weapons and armor while Wolves can't. So you put those attributes in the Warrior class instead of Enemy class. That way, Wolves inherit those attributes and won't waste memory with them. The second point is polymorphism. Every enemy has a different behaviour. Wolves might try to surround the player and attack in packs while Warriors might try to lure the player to traps. The class Enemy has a method called Behave that does not need to be implemented. Warriors and Wolves re-implement that method on their classes. That way, when you iterate the vector of enemies and call the method Behave, each of them will behave in their specific way.
11th Apr 2017, 4:13 AM
Denis Felipe
Denis Felipe - avatar
0
Thanks so much. :)
12th Apr 2017, 1:05 AM
Green