Skip to content
BENIBIX Learn

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.

Development🧭 Programming Paradigms

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.

Example
Jack has an Animal class with a breathe method. His Cat class inherits it, so every cat can breathe without new code.
Why it matters
It is the most common way to share code between related classes in OOP.
Full entry
Development🧭 Programming Paradigms

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.

Example
Lucy's app keeps a list of shapes. One loop calls area() on each, and circles and squares each give the right answer.
Why it matters
It lets you add a new kind of thing without changing the code that uses it.
Full entry

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.