OSPython.014: Polymorphism Basics

Black and solid neon-green OSPython.014 Polymorphism Basics cover with Animal, Dog, and Cat class diagram

Polymorphism means “many forms.” In Python, it lets the same method name or operation work with different kinds of objects.

In OSPython.013: Inheritance Basics, we learned that child classes can inherit and override behavior. Polymorphism builds directly on that idea: different objects can respond to the same method call in their own way.

One method name, different results

class Dog:
    def speak(self):
        return "Woof!"

class Cat:
    def speak(self):
        return "Meow!"

dog = Dog()
cat = Cat()

print(dog.speak())
print(cat.speak())

Both objects understand speak(), but each gives a different result. That is the basic idea: one common operation, multiple forms of behavior.

Video 1: Polymorphism, overriding, and duck typing

This dedicated Python tutorial covers polymorphism, method overriding, method overloading, and duck typing.

Polymorphism with inheritance

class Animal:
    def speak(self):
        return "Some sound"

class Dog(Animal):
    def speak(self):
        return "Woof!"

class Cat(Animal):
    def speak(self):
        return "Meow!"

Dog and Cat both inherit from Animal, then override speak(). The same method name now has a form appropriate to each child class.

Put different objects in one loop

animals = [Dog(), Cat()]

for animal in animals:
    print(animal.speak())

The loop does not need separate instructions for dogs and cats. It simply asks each object to speak(). Python uses the method belonging to that object.

Video 2: Polymorphism explained

CodeLucky focuses directly on Python polymorphism, method overriding, and duck typing.

Gaming example

class Player:
    def attack(self):
        return "Basic attack"

class Warrior(Player):
    def attack(self):
        return "Sword attack"

class Mage(Player):
    def attack(self):
        return "Magic attack"

team = [Warrior(), Mage()]

for player in team:
    print(player.attack())

The game loop can call attack() on every player without needing to know the exact character type first. Each class supplies the behavior that makes sense for it.

Simple Bitcoin mining example

class Machine:
    def status(self):
        return "Machine online"

class AirMiner(Machine):
    def status(self):
        return "Air-cooled miner online"

class HydroMiner(Machine):
    def status(self):
        return "Hydro miner online"

machines = [AirMiner(), HydroMiner()]

for machine in machines:
    print(machine.status())

The monitoring loop uses one simple status() call. Each machine type can return its own message. The new concept is polymorphism—not advanced mining telemetry.

Duck typing

Python can also use polymorphism without a shared parent class. If an object provides the method your code needs, Python can often use it. This style is commonly called duck typing.

class Phone:
    def play(self):
        return "Playing on phone"

class Console:
    def play(self):
        return "Playing on console"

def start_game(device):
    print(device.play())

start_game(Phone())
start_game(Console())

start_game() does not ask whether the object is a phone or console. It only uses the play() behavior that both objects provide.

Video 3: Polymorphism in Python

SDET-QA’s Python polymorphism tutorial reinforces polymorphic functions, method overriding, inheritance, and duck typing.

Polymorphism vs. inheritance

  • Inheritance describes a relationship between classes and lets a child reuse a parent’s behavior.
  • Overriding lets a child replace an inherited method with its own version.
  • Polymorphism lets code use a common operation while different objects provide different behavior.
  • Duck typing can provide polymorphic behavior even when the classes do not share a parent.

Common beginner mistakes

  • Thinking every polymorphic example must use inheritance.
  • Giving child methods different names when the goal is to share one common interface.
  • Forgetting that the object determines which overridden method runs.
  • Building complicated class trees when two simple classes would explain the idea better.

Practice

  1. Create classes named BasketballPlayer and FootballPlayer.
  2. Give both classes a method named play().
  3. Return a different message from each version of play().
  4. Put one object from each class into a list.
  5. Loop through the list and call play() without checking the object type.

Key takeaway

Polymorphism lets one piece of code work with different objects through a shared operation. The object itself supplies the appropriate behavior. If inheritance or method overriding feels unclear, review OSPython.013 before moving forward.

One response to “OSPython.014: Polymorphism Basics”

  1. […] OSPython.014: Polymorphism Basics, we saw different objects respond to the same method name in different ways. Encapsulation focuses […]

    Like

Leave a comment