Inheritance vs Polymorphism
People think inheritance and polymorphism are one idea. They are not, though they often show up together. Inheritance is how one class gets the parts of another. Polymorphism is one method name that does the right thing for each kind of object. Python can have it with no parent class at all. Each object just needs the method.
Inheritance
A new class that gets all of an old class's parts, then adds its own.
Inheritance lets a new class take on all the data and methods of a class that exists. Then it can add or change some. The old class is called the parent, and the new one is the child. A Cat class can get breathe from an Animal class and add its own meow. It saves writing the same code twice. Long chains of it, though, get hard to follow.
Polymorphism
One name that works on many kinds of thing, each doing it its own way.
Polymorphism means one name can work on many kinds of thing. Each kind does it its own way. You call area() on a circle and on a square, and each works out its own area. The code that calls it does not need to know which shape it has. Python's len works on text, lists and more for the same reason. It lets one loop handle a mix of objects.
In one line
- Inheritance
- A new class that gets all of an old class's parts, then adds its own.
- Polymorphism
- One name that works on many kinds of thing, each doing it its own way.