Understanding Polymorphism in Object-Oriented Programming

polymorphism n.w
1 / 10
Embed
Share

Explore the concept of polymorphism in programming, where methods, functions, and operators with the same name can be executed on different objects or classes. Learn how polymorphism is utilized with examples in Python, including function polymorphism and class polymorphism. Discover how inheritance and polymorphism work together to create versatile and reusable code structures.

  • Polymorphism
  • Object-Oriented Programming
  • Python
  • Inheritance
  • Versatile Code

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Polymorphism 1

  2. Objectives O This chapter introduces 1. Function polymorphism Class polymorphism Inheritance class polymorphism 2. 3. 2

  3. Polymorphism O Polymorphism means "many forms", and in programming it refers to methods, functions, operators with the same name that can be executed on many objects or classes 3

  4. Function Polymorphism O An example of a Python function that can be used on different objects is the len() function O For strings len() returns the number of characters O x = "Hello World! O print(len(x)) 4

  5. Function Polymorphism O For tuples len() returns the number of items in the tuple O mytuple = ("apple", "banana", "cherry") O print(len(mytuple)) O For dictionaries len() returns the number of key/value pairs in the dictionary thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(len(thisdict)) 5

  6. Class Polymorphism O Polymorphism methods, where we can have multiple classes with the same method name is often used in Class 6

  7. class Car: def __init__(self, brand, model): self.brand = brand self.model = model def move(self): print("Drive!") Example #Create a Car class car1 = Car("Ford", "Mustang") #Create a Boat class boat1 = Boat("Ibiza", "Touring 20") #Create a Plane class plane1 = Plane("Boeing", "747") class Boat: def __init__(self, brand, model): self.brand = brand self.model = model def move(self): print("Sail!") for x in (car1, boat1, plane1): x.move() class Plane: def __init__(self, brand, model): self.brand = brand self.model = model def move(self): print("Fly!") Because execute the same method for all three classes of polymorphism we can 7

  8. Inheritance Class Polymorphism O We can use polymorphism with inheritance O If we use the example in the previous slide and make a parent class called Vehicle, and make Car, Boat, Plane child classes of Vehicle, the child classes inherits the Vehicle methods, but can override them 8

  9. class Vehicle: def __init__(self, brand, model): self.brand = brand self.model = model Example def move(self): print("Move!") #Create a Car object car1 = Car("Ford", "Mustang") #Create a Boat object boat1 = Boat("Ibiza", "Touring 20") #Create a Plane object plane1 = Plane("Boeing", "747") for x in (car1, boat1, plane1): print(x.brand) print(x.model) x.move() class Car(Vehicle): pass class Boat(Vehicle): def move(self): print("Sail!") class Plane(Vehicle): def move(self): print("Fly!") The Boat and Plane classes override the move() method Because of polymorphism execute the same method for all classes we can 9

  10. O : O https://www.w3schools.com/python/python_ polymorphism.asp 10

More Related Content