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

Preview:

Citation preview

Polymorphism.

Chapter 11.

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

Parent class Mammal

Child class Dog

dog example

Child class Cat

cat example

isinstance(object, classname)If an object is an instance of classname or its subclass,

the function returns true.

Polymorphism example

Program output: polymorphism example

Error sending the show_species() message to a string

Solution 2

Chapter 11 Review

Checkpoint 11.3What does a subclass inherit from its superclass?

ANS.: Data attributes and methods.

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.

Checkpoint 11.5What will the following display?

Answer

Ex. 11.2What will the following display?

Answer

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.

Answer

Recommended