Python Polymorphism

Polymorphism Definition

Polymorphism means same function name, different behaviors, depending on class or input.

Two types in Python:

TypeMeaning
Compile-time (Overloading)Same function, different parameters (simulated in Python)
Runtime (Overriding)Child class modifies parent method

Each object responds differently to the same speak() call.


Python doesn’t support true overloading, so we use default parameters.


Same function → different behavior based on object type.


Child classes provide unique logic for inherited structure.


Real-World Example (Combined)

Explanation

  • Admin & Customer override the login() method.
  • Same method name → different behaviors based on object type.

Summary

FeatureWhat It DoesExample
InheritanceReuse parent class featuresclass B(A)
Multiple InheritanceChild from multiple parentsclass C(A,B)
PolymorphismSame method, different behaviorOverriding
OverridingModify parent methodRedefine in child
OverloadingSame name, different argumentsDefault parameters

Leave a Comment