0% found this document useful (0 votes)
5 views5 pages

Polymorphism

Uploaded by

okatakyieba09
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)
5 views5 pages

Polymorphism

Uploaded by

okatakyieba09
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

Mark Name

___/ Class
Polymorphism Date

1. What is polymorphism? [1]

Methods/functions/operators with the same name that can be operated on many


objects or classes

2. What is a derived class? [1]

A class that inherits data members from a base class

3. Create an animal class. This will be a parent class for later questions.[1]

a. The class will have the following attributes

Name: A string representing the name of the animal.


Age: An integer to represent the age of the animal.
Species: A string to denote the species of the animal.
Colour: A string representing the color of the animal.
Weight: A float to represent the weight of the animal, typically in kilograms or pounds.
Gender: A string to indicate the gender of the animal.
Habitat: A string to describe the natural habitat of the animal (e.g., forest, savannah, aquatic).
Diet: A string or list to describe the typical diet of the animal (e.g., carnivore, herbivore,
omnivore).
Health Status: A string or structured data to represent the health status of the animal, which
could include information about any illnesses or injuries.
Domesticated: A boolean to indicate whether the animal is domesticated or wild.

b. The derived classes will have the following attiributes


Dog Class
Breed: The breed of the dog (e.g., Labrador, German Shepherd).
Barking Volume: A measure of how loud the dog barks, possibly on a scale.
Trainability: A measure or description of how easy or difficult it is to train the dog.
Loyalty Level: An indicator of the dog's loyalty to its owner or family.

Cat Class
Breed: The breed of the cat (e.g., Siamese, Persian).
Indoor/Outdoor: A boolean indicating whether the cat is an indoor cat, an outdoor cat, or both.
Hunting Skill: A measure or description of the cat's ability to hunt.
Independence Level: An indicator of how independent the cat is.

Bird Class
Species: The specific species of the bird (e.g., Parrot, Canary).
Wing Span: The length of the bird's wingspan.
Migratory Pattern: Information about whether the bird is migratory and details of its migration.
Song Variety: A description or measure of the variety and complexity of the bird's song.

c. Create a method for animal where an animal will make a noise


(speak) “Rwar”. The speak method cat is meow bark for dog and
squark for bird.
4. Discuss the benefits of polymorphism in relation to OOP?

The programmer can reuse methods and properties in classes, which


saves them time when writing lengthy programs. Also it’s easy to debug
since the different methods are under the same name.

5. Question: Object Creation and Method Invocation in an Object-Oriented


Hierarchy

● Given the following object-oriented hierarchy where Animal is the


base class and Bird, Cat, and Dog are derived classes, write a Python
program that creates instances of these classes and demonstrates
polymorphism.
● The Animal class has basic attributes like name, age, and species, and
a method speak which is overridden in each derived class.
● The Bird class has an additional attribute wing_span.

● The Cat class includes an additional attribute independence_level.

● The Dog class includes an additional attribute loyalty_level.

● Tasks:

a. Object Creation: Create one object of each class (Animal, Bird,


Cat, Dog) with appropriate attributes. For instance, create a Dog
object with a loyalty_level, a Cat object with an
independence_level, etc.
b. Method Invocation: Call the speak method on each object and
print the result.

c. Polymorphism Demonstration: Create a list of Animal objects that


includes instances of Bird, Cat, and Dog. Iterate over this list and
call the speak method on each object to demonstrate
polymorphism.

You might also like