Explanation

  • Dynamic Binding (also known as Late Binding) refers to the process where the method to be called is determined at runtime, rather than at compile-time. This is a key feature of polymorphism in OOP, where method calls are resolved based on the object’s type at runtime.

Steps

  • Define a method in the base class and override it in the derived class.
  • When a method is called, Python determines at runtime which version of the method to invoke, based on the actual object type.
class Animal:
    def speak(self):
        print("Animal speaks")
 
class Dog(Animal):
    def speak(self):
        print("Woof!")
 
class Cat(Animal):
    def speak(self):
        print("Meow!")
 
# Late binding: The method is called based on the object's type at runtime
animals = [Dog(), Cat()]
for animal in animals:
    animal.speak()  # Output: Woof! \n Meow!