

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Inheritance
incomplete
2: When to Inherit
incomplete
3: Inheritance Hierarchy
incomplete
4: Inheritance Quiz
incomplete
5: Multiple Children
incomplete
6: Adding a Wizard
incomplete
7: Wide Not Deep
incomplete
8: Dragons
incomplete
9: Dragons Fight
incomplete
10: Inheritance Practice
incomplete
11: Inheritance Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Inheritance is a powerful tool, but do not overuse it (looking at you, Java devs). The rule of thumb is:
A should only inherit from B if A is always a B.
For example:
Cat should inherit from Animal because a cat is always an animal.Truck should inherit from Vehicle because a truck is always a vehicle.Lane should inherit from BadDotaPlayers because Lane is always bad at DotA.But these are not true, and you'll end up with a mess if you try to force them:
Cat should not inherit from Dog because a cat is not always a dog.Animal should not inherit from Cat because an animal is not always a cat.Allan should not inherit from Employed because Allan is not always employed.When a child class inherits from a parent, it inherits everything. If you only want to share some functionality, inheritance should not be the tool you use. Just share some functions, or maybe make a new parent class that both classes can inherit from.