9 5 Polymorphism Explained
Key Concepts
Polymorphism in Python involves several key concepts:
- Method Overriding
- Method Overloading
- Duck Typing
- Polymorphism in Inheritance
- Polymorphism with Abstract Classes
1. Method Overriding
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. This allows the subclass to customize behavior.
Example:
class Animal: def speak(self): return "Animal sound" class Dog(Animal): def speak(self): return "Woof!" dog = Dog() print(dog.speak()) # Output: Woof!
Analogy: Think of method overriding as a child choosing to do something differently than their parent.
2. Method Overloading
Method overloading allows a class to have more than one method with the same name but different parameters. Python does not support traditional method overloading, but it can be achieved using default arguments or variable-length arguments.
Example:
class Calculator: def add(self, a, b=0): return a + b calc = Calculator() print(calc.add(5)) # Output: 5 print(calc.add(5, 3)) # Output: 8
Analogy: Think of method overloading as a tool that can be used in different ways depending on the inputs provided.
3. Duck Typing
Duck typing is a concept where the type or class of an object is less important than the methods it defines. If an object behaves in the expected way, it is considered valid, regardless of its type.
Example:
class Duck: def quack(self): return "Quack!" class Person: def quack(self): return "I'm quacking like a duck!" def make_quack(entity): print(entity.quack()) duck = Duck() person = Person() make_quack(duck) # Output: Quack! make_quack(person) # Output: I'm quacking like a duck!
Analogy: Think of duck typing as the saying, "If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck."
4. Polymorphism in Inheritance
Polymorphism in inheritance allows objects of different subclasses to be treated as objects of a common superclass. This is often used to create flexible and reusable code.
Example:
class Animal: def speak(self): pass class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" def animal_sound(animal): print(animal.speak()) dog = Dog() cat = Cat() animal_sound(dog) # Output: Woof! animal_sound(cat) # Output: Meow!
Analogy: Think of polymorphism in inheritance as a universal remote that can control different devices, each with its own specific functions.
5. Polymorphism with Abstract Classes
Polymorphism can also be achieved using abstract classes and methods. Abstract classes define a common interface that must be implemented by subclasses.
Example:
from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def speak(self): pass class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" def animal_sound(animal): print(animal.speak()) dog = Dog() cat = Cat() animal_sound(dog) # Output: Woof! animal_sound(cat) # Output: Meow!
Analogy: Think of abstract classes as a blueprint that ensures all buildings (subclasses) follow a certain structure.
Putting It All Together
By understanding and using these concepts effectively, you can create more flexible and reusable Python code.
Example:
class Animal: def speak(self): pass class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" def animal_sound(animal): print(animal.speak()) dog = Dog() cat = Cat() animal_sound(dog) # Output: Woof! animal_sound(cat) # Output: Meow!