polymorphism. · polymorphism - the ability to present the same interface for differing data types
TRANSCRIPT
![Page 1: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/1.jpg)
Polymorphism.
Chapter 11.
![Page 2: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/2.jpg)
POLYMORPHISM - the ability to present the same interface for differing data types.
![Page 3: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/3.jpg)
Parent class Mammal
![Page 4: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/4.jpg)
Child class Dog
![Page 5: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/5.jpg)
dog example
![Page 6: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/6.jpg)
Child class Cat
![Page 7: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/7.jpg)
cat example
![Page 8: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/8.jpg)
isinstance(object, classname)If an object is an instance of classname or its subclass,
the function returns true.
![Page 9: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/9.jpg)
Polymorphism example
![Page 10: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/10.jpg)
Program output: polymorphism example
![Page 11: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/11.jpg)
Error sending the show_species() message to a string
![Page 12: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/12.jpg)
Solution 2
![Page 13: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/13.jpg)
Chapter 11 Review
![Page 14: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/14.jpg)
Checkpoint 11.3What does a subclass inherit from its superclass?
ANS.: Data attributes and methods.
![Page 15: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/15.jpg)
Checkpoint 11.4Below is the first line of the class definition. What is the name of the superclass? What is the name of the subclass?
class Canary (Bird):
ANS.: Bird is the superclass and Canary is the subclass.
![Page 16: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/16.jpg)
Checkpoint 11.5What will the following display?
![Page 17: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/17.jpg)
Answer
![Page 18: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/18.jpg)
Ex. 11.2What will the following display?
![Page 19: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/19.jpg)
Answer
![Page 20: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/20.jpg)
11.3 ExerciseLook at the following class definition:
Write the code for a class named Cola that is the subclass of the Beverage class. The Cola class’s __init__ method should call the Beverage class’s __init__ method, passing ‘cola’ as an argument.
![Page 21: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types](https://reader035.vdocument.in/reader035/viewer/2022070803/5f0302cc7e708231d40718cd/html5/thumbnails/21.jpg)
Answer