PYTHON PROGRAMMING: The ultimate guide from a beginner to expert, all you need to know about python, tools, tricks, best practices, and advanced features by Campbell Clive

PYTHON PROGRAMMING: The ultimate guide from a beginner to expert, all you need to know about python, tools, tricks, best practices, and advanced features by Campbell Clive

Author:Campbell, Clive [Campbell, Clive]
Language: eng
Format: epub
Published: 2019-08-15T16:00:00+00:00


Polymorphism

In Python, polymorphism refers to the ability to use a shared interface for several data types.

Start IDLE.

Navigate to the File menu and click New Window.

Type the following line of code:

class Tilapia:

def swim(self):

print("Tilapia can swim")

def fly(self):

print("Tilapia cannot fly")

class Shark:

def jump(self):

print("Shark can't fly")

def swim(self):

print("Shark can swim")

def jumping_test(fish):

fish.jump()

bonny = Tilapia()

biggy = Shark()

jumping_test(bonny)

jumping_test(biggy)

Explanation

The program above has defined two classes Tilapia and Shark all of which share the method jump() even though they have different functions.

By creating common interface jumping_test() we allowed polymorphism in the program above. We then passed objects bonny and biggy in the jumping_test() function.

Work to do

In a doctor consultation room suggest the class and objects in a programming context.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.