Question: Feel free to use the exact code from slides 2 1 - 2 6 . Then, add an additional mammal to the animals.py file as
Feel free to use the exact code from slides
Then, add an additional mammal to the animals.py file as well as make sure you update your polymorphismdemo file, the result should display the new animal name and its unique sound.
Please upload screen captures of the code and testing result for credit.
Let's create a new script and call it animals.pv
The Mammal class represents a generic mammal.
class Mammal:
# The init method accepts an argument for
# the mammal's species.
def initself species:
self.species species
# The showspecies method displays a message
# indicating the mammal's species.
def showspectesself:
printI am a self.species
# The makesound method is the mammal's
# way of making a generic sound.
def makesoundself:
printGrrrrr
Now let's add the dog class to the animals.py file
# The Dog class is a subclass of the Mammal Superclass.
class DogMammal:
# The init method calls the superclass's
# init method passing 'Dog' as the species.
def initself:
Mammal.initself 'Dog'
# The makesound method overrides the superclass's
# makesound method.
def makesoundself:
printWoof Woof!
Now let's create a new file called polymorphismdemo.py
This program demonstrates polymorphism.
import animals
def main:
# Create a Mammal object, a Dog object, and
# a Cat object.
mammal animals.Mammalregular animal'
dog animals.Dog
cat animals.Cat
# Display information about each one.
printHere are some animals and'
printthe sounds they make.
print
showmammalinfomammal
print
showmammalinfodog
print
showmammalinfocat Now let's add an update to our polymorphismdemo file for testing:
showmammalinfodog
print
showmammalinfocat
print
showmammalinfobird
The showmammalinfo function accepts an object
as an argument, and calls its showspecies
and makesound methods.
def showmammalinfocreature:
if isinstancecreature animals.Mammal:
creature.showspecies
creature.makesound
else:
printThat is not a Mammal!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
