What is inheritance in programming
We made "one thing" with objects (Lesson 12). But if you make a dog, a cat, and a bird as objects, name, age, and eating keep repeating in exactly the same way. Inheritance means gathering those shared parts in one place and letting each of them inherit it.
Parts that keep repeating
A dog, a cat, and a bird can all be made as objects.
But put the three side by side and name, age, and eating appear in all three, exactly the same.
It's like writing the same thing three times, and the more kinds you add, the bigger the overlap gets.
Press "show overlap" and the labels shared by the dog, cat, and bird light up together.
The overlapping parts are clear at a glance.
If you write the same thing again in every object like this, fixing one means fixing them all, one by one.
So where should these shared parts go?
Gather the shared parts upward
Pull the overlapping name, age, and eating up into one place above.
In that spot, you put a parent called "animal."
Now the shared parts live only in animal, and the dog, cat, and bird become its children below.
Press "gather shared parts up" and the overlapping labels rise to form a single "animal" parent.
With the shared parts gathered in one place, the children are much lighter.
But don't the children now lack name, age, and eating?
They haven't disappeared; the children get them from the parent.
Inheriting what the parent has
A child automatically has everything the parent has.
The dog inherits animal's name, age, and eating without writing them again.
So the dog is already a proper animal. It received them; it didn't copy them.
Pick a child and the parent's labels come down and fill it in, just as they are.
The dog, the cat, and the bird all inherited the same things from their parent, animal.
But if the three were exactly the same, they wouldn't be different kinds.
A dog barks and a bird flies, so how do you add things like that?
Add your own on top of what you inherit
Keep the inherited shared parts as they are, and put your own on top.
The dog adds "bark," and the bird adds "fly."
They share a parent, but what they add is different, so the dog and the bird become different kinds.
Press "add its own" and the child gets a label of its own, in a different color from the inherited ones.
Now the dog is "animal's shared parts + bark."
What it received and what it added have become one.
Shared parts in the parent, differences in the child. Thanks to this structure, one last big benefit follows.
Fix it once, and they all change
Here's the real power of inheritance.
Add "sleep" to the parent, animal, and the dog, the cat, and the bird can all sleep at once.
They all change without your fixing each child, and with the duplication gone, mistakes drop too.
Starting from values, we made "things" with objects and arranged them neatly with inheritance.
We now have a fairly large program in hand.
But programs always go wrong somewhere, so in the next lesson we'll learn how to find and fix what's wrong.