polymorphism
One call behaving differently depending on the class of the object it is made on.
In more detail
When a parent's method calls self.area(), self is the actual object, so a subclass's override of area runs. Different classes can therefore answer the same call in their own way without the caller knowing which class it holds. Overriding a method in a subclass is how Python achieves it.
Example
for shape in (Square(3), Circle(2)):
print(shape.area())Related terms
- method overriding — Defining a method in a subclass with the same name as one in its parent, so the subclass's version is used instead.
- inheritance — Declaring one class or type as a specialised version of another, so it receives the parent's members and can be used wherever the parent is expected.
- class — A blueprint in code that bundles data and the functions that work on it, from which objects are made.
Taught in
- Inheritance, super() And Polymorphism — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep