KEMBAR78
Polymorphism Program | PDF
0% found this document useful (0 votes)
9 views5 pages

Polymorphism Program

The document demonstrates polymorphism in Python through inheritance, showcasing how different animal classes can override a common method to produce unique sounds. It also illustrates method overriding with shape classes, where specific shapes implement their own area calculations and facts. The examples highlight the flexibility of polymorphism in object-oriented programming.

Uploaded by

nainalashalini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Polymorphism Program

The document demonstrates polymorphism in Python through inheritance, showcasing how different animal classes can override a common method to produce unique sounds. It also illustrates method overriding with shape classes, where specific shapes implement their own area calculations and facts. The examples highlight the flexibility of polymorphism in object-oriented programming.

Uploaded by

nainalashalini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

polymorphism using inheritance in Python


class Animal:

def __init__(self, name):

self.name = name

def speak(self):

return "Generic animal sound"

class Dog(Animal):

def speak(self):

return "Woof!"

class Cat(Animal):

def speak(self):

return "Meow!"

def animal_sound(animal):

return animal.speak()

# Instances of different classes

generic_animal = Animal("Generic Animal")

dog = Dog("Buddy")

cat = Cat("Whiskers")

# Polymorphism in action

print(f"{generic_animal.name} says: {animal_sound(generic_animal)}")

print(f"{dog.name} says: {animal_sound(dog)}")

print(f"{cat.name} says: {animal_sound(cat)}")


Output:
Code
Generic Animal says: Generic animal sound
Buddy says: Woof!
Whiskers says: Meow!

2.Polymorphism in Class Methods

class Cat:

def __init__(self, name, age):

self.name = name

self.age = age

def info(self):

print(f"I am a cat. My name is {self.name}. I am {self.age} years old.")

def make_sound(self):

print("Meow")

class Dog:

def __init__(self, name, age):

self.name = name

self.age = age

def info(self):

print(f"I am a dog. My name is {self.name}. I am {self.age} years old.")


def make_sound(self):

print("Bark")

cat1 = Cat("Kitty", 2.5)

dog1 = Dog("Fluffy", 4)

for animal in (cat1, dog1):

animal.make_sound()

animal.info()

animal.make_sound()

Output

Meow

I am a cat. My name is Kitty. I am 2.5 years old.

Meow

Bark

I am a dog. My name is Fluffy. I am 4 years old.

Bark

3.Method Overriding

from math import pi

class Shape:
def __init__(self, name):

self.name = name

def area(self):

pass

def fact(self):

return "I am a two-dimensional shape."

def __str__(self):

return self.name

class Square(Shape):

def __init__(self, length):

super().__init__("Square")

self.length = length

def area(self):

return self.length**2

def fact(self):

return "Squares have each angle equal to 90 degrees."

class Circle(Shape):

def __init__(self, radius):

super().__init__("Circle")

self.radius = radius
def area(self):

return pi*self.radius**2

a = Square(4)

b = Circle(7)

print(b)

print(b.fact())

print(a.fact())

print(b.area())

Output

Circle

I am a two-dimensional shape.

Squares have each angle equal to 90 degrees.

153.93804002589985

You might also like