Codele / Advanced programming
Inheritance
A new class does not have to start from nothing. It can begin from an existing class, get everything that class has, and then add or change a little.
- Inheritance lets a new class start from an existing one and add or change only what is different.
- The existing class is the parent (or base class). The new one is the child (or derived class). A child gets all of its parent’s data and methods.
- If a method is not in the child, the computer looks in the parent. Then the parent’s parent, and so on up the family.
- Use it for a true is-a: a Dog is an Animal. If the sentence sounds wrong, do not inherit.
- Go has no classes or inheritance. A struct can embed another struct to get its methods, which looks the same when you call them. Python, JavaScript, Java, C# and C++ write the child as Dog(Animal), extends or a colon.
Loading the playable lesson…