Top Banner
Polymorphism. Chapter 11.
21

Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Jun 06, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Polymorphism.

Chapter 11.

Page 2: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

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.

Parent class Mammal

Page 4: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Child class Dog

Page 5: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

dog example

Page 6: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Child class Cat

Page 7: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

cat example

Page 8: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

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.

Polymorphism example

Page 10: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Program output: polymorphism example

Page 11: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Error sending the show_species() message to a string

Page 12: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Solution 2

Page 13: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Chapter 11 Review

Page 14: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

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.

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.

Checkpoint 11.5What will the following display?

Page 17: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Answer

Page 18: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Ex. 11.2What will the following display?

Page 19: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

Answer

Page 20: Polymorphism. · POLYMORPHISM - the ability to present the same interface for differing data types.

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.

Answer