Polymorphism with Methods

Use polymorphism by defining the same method name in different classes.

BeginnerTopic: Object-Oriented Programs
Back

Python Polymorphism with Methods Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to demonstrate polymorphism

class Cat:
    def speak(self):
        print("Meow")


class Dog:
    def speak(self):
        print("Woof")


def animal_speak(animal):
    animal.speak()


animal_speak(Cat())
animal_speak(Dog())
Output
Meow
Woof

Understanding Polymorphism with Methods

Different classes implement the same 'speak' interface, and the function 'animal_speak' works with any such object.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents