[go: up one dir, main page]

0% found this document useful (0 votes)
7 views3 pages

Big Data EX 4

The document outlines a Python program that demonstrates inheritance and polymorphism through the creation of a base class Animal and derived classes Dog and Cat. It includes an algorithm detailing the steps to define classes, override methods, and demonstrate polymorphism. The program successfully implements these concepts and verifies the output.

Uploaded by

giripriya1829
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)
7 views3 pages

Big Data EX 4

The document outlines a Python program that demonstrates inheritance and polymorphism through the creation of a base class Animal and derived classes Dog and Cat. It includes an algorithm detailing the steps to define classes, override methods, and demonstrate polymorphism. The program successfully implements these concepts and verifies the output.

Uploaded by

giripriya1829
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/ 3

EX NO: PROGRAM TO PERFORM INHERITANCE

DATE : AND POLYMORPHISM


AIM:

To write a Python program to perform inheritance and polymorphism.

ALGORITHM:

1. Start the program.


2. Define the base class Animal with a speak() method.
3. Create derived classes Dog and Cat, overriding the speak() method with specific
behaviors.
4. Define a function make_animal_speak() that calls the speak() method of the given
animal object.
5. Instantiate objects of Dog and Cat classes.
6. Call the speak() method directly and through make_animal_speak() to demonstrate
polymorphism.

CODE:

class Animal:
def speak(self):
print("The animal makes a sound.")

class Dog(Animal):
def speak(self):
print("The dog barks.")

class Cat(Animal):
def speak(self):
print("The cat meows.")

def make_animal_speak(animal):
animal.speak()

if _name_ == "_main_":
dog = Dog()
cat = Cat()
print("Dog's action:")
dog.speak()
print("Cat's action:")
cat.speak()
print("Polymorphism demonstration:")
make_animal_speak(dog)
make_animal_speak(cat)
OUTPUT:
RESULT:

Thus the program to perform inheritance and polymorphism is implemented and the
output is verified successfully.

You might also like