0
0
Pythonprogramming~10 mins

Polymorphism through inheritance in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Polymorphism through inheritance
Define Base Class
Define Derived Classes
Create Objects of Derived Classes
Call Same Method on Each Object
Each Object Uses Its Own Method Version
Output Respective Results
This flow shows how a base class defines a method, derived classes override it, and calling the method on different objects runs their own version.
Execution Sample
Python
class Animal:
    def sound(self):
        return "Some sound"

class Dog(Animal):
    def sound(self):
        return "Bark"

class Cat(Animal):
    def sound(self):
        return "Meow"

animals = [Dog(), Cat()]
for a in animals:
    print(a.sound())
This code creates a base class Animal and two derived classes Dog and Cat, each with their own sound method. It prints the sound of each animal.
Execution Table
StepActionObjectMethod CalledResultOutput
1Create Dog objectDog instance---
2Create Cat objectCat instance---
3Call sound() on DogDog instanceDog.sound()"Bark"Bark
4Call sound() on CatCat instanceCat.sound()"Meow"Meow
5End of loop----
💡 All objects processed, loop ends.
Variable Tracker
VariableStartAfter 1After 2Final
animals[][Dog()][Dog(), Cat()][Dog(), Cat()]
aNoneDog instanceCat instanceCat instance
Key Moments - 2 Insights
Why does calling sound() on different objects print different results?
Because each derived class overrides the sound() method, so when called on an object, Python uses the method version of that object's class (see execution_table steps 3 and 4).
Is the base class method sound() ever called here?
No, because both Dog and Cat override sound(), so their versions are used instead (execution_table rows 3 and 4). If a derived class did not override it, the base class method would run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when sound() is called on the Dog object?
ASome sound
BBark
CMeow
DError
💡 Hint
Check execution_table row 3 where Dog.sound() returns 'Bark'.
At which step does the Cat object’s sound() method get called?
AStep 4
BStep 1
CStep 3
DStep 2
💡 Hint
Look at execution_table row 4 for Cat.sound() call.
If the Cat class did not have its own sound() method, what would be the output when calling sound() on a Cat object?
ABark
BMeow
CSome sound
DError
💡 Hint
Without override, the base class method runs, see key_moments explanation.
Concept Snapshot
Polymorphism through inheritance means derived classes can have methods with the same name as the base class.
When calling these methods on objects, Python runs the version from the object's class.
This lets different objects respond differently to the same method call.
Use method overriding in derived classes to customize behavior.
Call methods on base class references to get polymorphic behavior.
Full Transcript
This example shows polymorphism using inheritance in Python. We define a base class Animal with a method sound(). Two derived classes Dog and Cat override this method with their own versions. We create objects of Dog and Cat and call sound() on each. The program prints 'Bark' for Dog and 'Meow' for Cat. This happens because Python uses the method version of the actual object's class, not just the base class. This is polymorphism: one method name, many behaviors depending on the object. If a derived class does not override the method, the base class version runs. This lets us write flexible code that works with different object types through a common interface.