Polymorphism
Polymorphism is the ability of different classes to be treated as instances of the same class through a common interface. It allows methods to do different things based on the object they’re acting upon, even though they share the same name.
Understanding Polymorphism
Section titled “Understanding Polymorphism”The word “polymorphism” comes from Greek meaning “many forms”. In programming, it means that objects of different types can be accessed through the same interface.
Types of Polymorphism
Section titled “Types of Polymorphism”- Duck Typing - “If it walks like a duck and quacks like a duck, it’s a duck”
- Method Overriding - Subclasses override parent methods
- Operator Overloading - Same operator works differently for different types
Duck Typing
Section titled “Duck Typing”class Dog: def speak(self): return "Woof!"
class Cat: def speak(self): return "Meow!"
class Robot: def speak(self): return "Beep boop!"
def make_sound(animal): """Function works with any object that has a speak() method""" print(animal.speak())
# All these work - they all have a speak() methoddog = Dog()cat = Cat()robot = Robot()
make_sound(dog) # "Woof!"make_sound(cat) # "Meow!"make_sound(robot) # "Beep boop!"// Java uses interfaces for polymorphismpublic interface Speakable { String speak();}
public class Dog implements Speakable { @Override public String speak() { return "Woof!"; }}
public class Cat implements Speakable { @Override public String speak() { return "Meow!"; }}
public class Robot implements Speakable { @Override public String speak() { return "Beep boop!"; }}
// Function works with any object that implements Speakablepublic class Main { public static void makeSound(Speakable speaker) { System.out.println(speaker.speak()); }
public static void main(String[] args) { Speakable dog = new Dog(); Speakable cat = new Cat(); Speakable robot = new Robot();
makeSound(dog); // "Woof!" makeSound(cat); // "Meow!" makeSound(robot); // "Beep boop!" }}Polymorphism Through Inheritance
Section titled “Polymorphism Through Inheritance”When classes inherit from a common base class, they can be used interchangeably:
class Vehicle: def __init__(self, brand: str, model: str, year: int): self.brand = brand self.model = model self.year = year
def start(self): """Base implementation""" return f"{self.brand} {self.model} started."
def get_info(self): return f"{self.brand} {self.model}, Year: {self.year}"
class Car(Vehicle): def start(self): """Polymorphic behavior - Car's version""" return f"{self.brand} {self.model} car started with a roar!"
class Motorcycle(Vehicle): def start(self): """Polymorphic behavior - Motorcycle's version""" return f"{self.brand} {self.model} motorcycle started with a vroom!"
class ElectricVehicle(Vehicle): def start(self): """Polymorphic behavior - Electric's version""" return f"{self.brand} {self.model} silently started (electric motor)"
def start_vehicle(vehicle: Vehicle): """Function accepts any Vehicle - polymorphism in action""" print(vehicle.start()) print(vehicle.get_info())
# All vehicles can be used interchangeablycar = Car("Toyota", "Camry", 2020)motorcycle = Motorcycle("Yamaha", "YZF-R3", 2021)electric = ElectricVehicle("Tesla", "Model 3", 2023)
start_vehicle(car) # Works - Car is a Vehiclestart_vehicle(motorcycle) # Works - Motorcycle is a Vehiclestart_vehicle(electric) # Works - ElectricVehicle is a Vehiclepublic class Vehicle { protected String brand; protected String model; protected int year;
public Vehicle(String brand, String model, int year) { this.brand = brand; this.model = model; this.year = year; }
// Base implementation public String start() { return brand + " " + model + " started."; }
public String getInfo() { return brand + " " + model + ", Year: " + year; }}
public class Car extends Vehicle { public Car(String brand, String model, int year) { super(brand, model, year); }
@Override public String start() { // Polymorphic behavior - Car's version return brand + " " + model + " car started with a roar!"; }}
public class Motorcycle extends Vehicle { public Motorcycle(String brand, String model, int year) { super(brand, model, year); }
@Override public String start() { // Polymorphic behavior - Motorcycle's version return brand + " " + model + " motorcycle started with a vroom!"; }}
public class ElectricVehicle extends Vehicle { public ElectricVehicle(String brand, String model, int year) { super(brand, model, year); }
@Override public String start() { // Polymorphic behavior - Electric's version return brand + " " + model + " silently started (electric motor)"; }}
// Function accepts any Vehicle - polymorphism in actionpublic class Main { public static void startVehicle(Vehicle vehicle) { System.out.println(vehicle.start()); System.out.println(vehicle.getInfo()); }
public static void main(String[] args) { // All vehicles can be used interchangeably Car car = new Car("Toyota", "Camry", 2020); Motorcycle motorcycle = new Motorcycle("Yamaha", "YZF-R3", 2021); ElectricVehicle electric = new ElectricVehicle("Tesla", "Model 3", 2023);
startVehicle(car); // Works - Car is a Vehicle startVehicle(motorcycle); // Works - Motorcycle is a Vehicle startVehicle(electric); // Works - ElectricVehicle is a Vehicle }}classDiagram
class Vehicle {
+brand: str
+model: str
+year: int
+start()* str
+get_info() str
}
class Car {
+start() str
}
class Motorcycle {
+start() str
}
class ElectricVehicle {
+start() str
}
Vehicle <|-- Car
Vehicle <|-- Motorcycle
Vehicle <|-- ElectricVehicle
note for Vehicle "Polymorphism:\nSame interface,\ndifferent implementations"
Real-World Example: Payment Processing
Section titled “Real-World Example: Payment Processing”class PaymentMethod: """Base class for payment methods""" def process_payment(self, amount: float) -> bool: raise NotImplementedError("Subclass must implement process_payment")
class CreditCard(PaymentMethod): def __init__(self, card_number: str, cvv: str): self.card_number = card_number self.cvv = cvv
def process_payment(self, amount: float) -> bool: """Process credit card payment""" print(f"Processing ${amount:.2f} via credit card ending in {self.card_number[-4:]}") # Credit card processing logic return True
class PayPal(PaymentMethod): def __init__(self, email: str): self.email = email
def process_payment(self, amount: float) -> bool: """Process PayPal payment""" print(f"Processing ${amount:.2f} via PayPal ({self.email})") # PayPal processing logic return True
class BankTransfer(PaymentMethod): def __init__(self, account_number: str): self.account_number = account_number
def process_payment(self, amount: float) -> bool: """Process bank transfer""" print(f"Processing ${amount:.2f} via bank transfer (Account: {self.account_number})") # Bank transfer logic return True
class ShoppingCart: """Shopping cart that accepts any payment method""" def __init__(self): self.items = [] self.total = 0.0
def add_item(self, item: str, price: float): self.items.append((item, price)) self.total += price
def checkout(self, payment_method: PaymentMethod) -> bool: """Polymorphic method - works with any PaymentMethod""" print(f"Checking out {len(self.items)} items, Total: ${self.total:.2f}") return payment_method.process_payment(self.total)
# Usage - polymorphism allows using different payment methods interchangeablycart = ShoppingCart()cart.add_item("Laptop", 999.99)cart.add_item("Mouse", 29.99)
# All payment methods work the same waycredit_card = CreditCard("1234567890123456", "123")bank_transfer = BankTransfer("ACC-12345")
cart.checkout(credit_card) # Workscart.checkout(paypal) # Workscart.checkout(bank_transfer) # Works// Base class for payment methodspublic abstract class PaymentMethod { public abstract boolean processPayment(double amount);}
public class CreditCard extends PaymentMethod { private String cardNumber; private String cvv;
public CreditCard(String cardNumber, String cvv) { this.cardNumber = cardNumber; this.cvv = cvv; }
@Override public boolean processPayment(double amount) { // Process credit card payment String lastFour = cardNumber.substring(cardNumber.length() - 4); System.out.printf("Processing $%.2f via credit card ending in %s%n", amount, lastFour); // Credit card processing logic return true; }}
public class PayPal extends PaymentMethod { private String email;
public PayPal(String email) { this.email = email; }
@Override public boolean processPayment(double amount) { // Process PayPal payment System.out.printf("Processing $%.2f via PayPal (%s)%n", amount, email); // PayPal processing logic return true; }}
public class BankTransfer extends PaymentMethod { private String accountNumber;
public BankTransfer(String accountNumber) { this.accountNumber = accountNumber; }
@Override public boolean processPayment(double amount) { // Process bank transfer System.out.printf("Processing $%.2f via bank transfer (Account: %s)%n", amount, accountNumber); // Bank transfer logic return true; }}
// Shopping cart that accepts any payment methodpublic class ShoppingCart { private java.util.List<String> items; private double total;
public ShoppingCart() { this.items = new java.util.ArrayList<>(); this.total = 0.0; }
public void addItem(String item, double price) { items.add(item); total += price; }
// Polymorphic method - works with any PaymentMethod public boolean checkout(PaymentMethod paymentMethod) { System.out.printf("Checking out %d items, Total: $%.2f%n", items.size(), total); return paymentMethod.processPayment(total); }}
// Usage - polymorphism allows using different payment methods interchangeablypublic class Main { public static void main(String[] args) { ShoppingCart cart = new ShoppingCart(); cart.addItem("Laptop", 999.99); cart.addItem("Mouse", 29.99);
// All payment methods work the same way PaymentMethod creditCard = new CreditCard("1234567890123456", "123"); PaymentMethod bankTransfer = new BankTransfer("ACC-12345");
cart.checkout(creditCard); // Works cart.checkout(paypal); // Works cart.checkout(bankTransfer); // Works }}Polymorphism with Abstract Classes
Section titled “Polymorphism with Abstract Classes”Using abstract classes ensures all implementations provide required methods:
from abc import ABC, abstractmethod
class Shape(ABC): """Abstract base class""" @abstractmethod def area(self) -> float: pass
@abstractmethod def perimeter(self) -> float: pass
class Rectangle(Shape): def __init__(self, width: float, height: float): self.width = width self.height = height
def area(self) -> float: return self.width * self.height
def perimeter(self) -> float: return 2 * (self.width + self.height)
class Circle(Shape): def __init__(self, radius: float): self.radius = radius
def area(self) -> float: import math return math.pi * self.radius ** 2
def perimeter(self) -> float: import math return 2 * math.pi * self.radius
class Triangle(Shape): def __init__(self, a: float, b: float, c: float): self.a = a self.b = b self.c = c
def area(self) -> float: # Heron's formula s = self.perimeter() / 2 return (s * (s - self.a) * (s - self.b) * (s - self.c)) ** 0.5
def perimeter(self) -> float: return self.a + self.b + self.c
def print_shape_info(shape: Shape): """Polymorphic function - works with any Shape""" print(f"Area: {shape.area():.2f}") print(f"Perimeter: {shape.perimeter():.2f}")
# All shapes can be used interchangeablyrectangle = Rectangle(5, 3)circle = Circle(4)triangle = Triangle(3, 4, 5)
print_shape_info(rectangle) # Worksprint_shape_info(circle) # Worksprint_shape_info(triangle) # Works// Abstract base classpublic abstract class Shape { public abstract double area(); public abstract double perimeter();}
public class Rectangle extends Shape { private double width; private double height;
public Rectangle(double width, double height) { this.width = width; this.height = height; }
@Override public double area() { return width * height; }
@Override public double perimeter() { return 2 * (width + height); }}
public class Circle extends Shape { private double radius;
public Circle(double radius) { this.radius = radius; }
@Override public double area() { return Math.PI * radius * radius; }
@Override public double perimeter() { return 2 * Math.PI * radius; }}
public class Triangle extends Shape { private double a; private double b; private double c;
public Triangle(double a, double b, double c) { this.a = a; this.b = b; this.c = c; }
@Override public double area() { // Heron's formula double s = perimeter() / 2; return Math.sqrt(s * (s - a) * (s - b) * (s - c)); }
@Override public double perimeter() { return a + b + c; }}
// Polymorphic function - works with any Shapepublic class Main { public static void printShapeInfo(Shape shape) { System.out.printf("Area: %.2f%n", shape.area()); System.out.printf("Perimeter: %.2f%n", shape.perimeter()); }
public static void main(String[] args) { // All shapes can be used interchangeably Rectangle rectangle = new Rectangle(5, 3); Circle circle = new Circle(4); Triangle triangle = new Triangle(3, 4, 5);
printShapeInfo(rectangle); // Works printShapeInfo(circle); // Works printShapeInfo(triangle); // Works }}Operator Overloading (Polymorphism)
Section titled “Operator Overloading (Polymorphism)”The same operator can work differently for different types:
class Vector: def __init__(self, x: float, y: float): self.x = x self.y = y
def __add__(self, other): """+ operator - polymorphic behavior""" if isinstance(other, Vector): return Vector(self.x + other.x, self.y + other.y) elif isinstance(other, (int, float)): return Vector(self.x + other, self.y + other) return NotImplemented
def __mul__(self, scalar): """* operator - polymorphic behavior""" return Vector(self.x * scalar, self.y * scalar)
def __str__(self): return f"Vector({self.x}, {self.y})"
v1 = Vector(1, 2)v2 = Vector(3, 4)
# Same + operator, different behaviorresult1 = v1 + v2 # Vector additionresult2 = v1 + 5 # Scalar additionresult3 = v1 * 3 # Scalar multiplication
print(result1) # Vector(4, 6)print(result2) # Vector(6, 7)print(result3) # Vector(3, 6)public class Vector { private double x; private double y;
public Vector(double x, double y) { this.x = x; this.y = y; }
// Add another vector public Vector add(Vector other) { return new Vector(this.x + other.x, this.y + other.y); }
// Add scalar to vector public Vector add(double scalar) { return new Vector(this.x + scalar, this.y + scalar); }
// Multiply vector by scalar public Vector multiply(double scalar) { return new Vector(this.x * scalar, this.y * scalar); }
@Override public String toString() { return String.format("Vector(%.1f, %.1f)", x, y); }}
// Usagepublic class Main { public static void main(String[] args) { Vector v1 = new Vector(1, 2); Vector v2 = new Vector(3, 4);
// Use methods instead of operators Vector result1 = v1.add(v2); // Vector addition Vector result2 = v1.add(5); // Scalar addition Vector result3 = v1.multiply(3); // Scalar multiplication
System.out.println(result1); // Vector(4.0, 6.0) System.out.println(result2); // Vector(6.0, 7.0) System.out.println(result3); // Vector(3.0, 6.0) }}Note: Java doesn’t support operator overloading for user-defined classes (except for + with strings). Use methods like add(), multiply(), etc. instead.
Real-World Example: Notification System
Section titled “Real-World Example: Notification System”class Notification: """Base notification class""" def send(self, message: str) -> bool: raise NotImplementedError("Subclass must implement send()")
class EmailNotification(Notification): def __init__(self, recipient: str): self.recipient = recipient
def send(self, message: str) -> bool: """Email-specific implementation""" print(f"Sending email to {self.recipient}: {message}") return True
class SMSNotification(Notification): def __init__(self, phone_number: str): self.phone_number = phone_number
def send(self, message: str) -> bool: """SMS-specific implementation""" print(f"Sending SMS to {self.phone_number}: {message[:50]}...") return True
class PushNotification(Notification): def __init__(self, device_id: str): self.device_id = device_id
def send(self, message: str) -> bool: """Push notification-specific implementation""" print(f"Sending push to device {self.device_id}: {message}") return True
class NotificationService: """Service that can use any notification type""" def __init__(self): self.notifications = []
def add_notification(self, notification: Notification): """Add any notification type""" self.notifications.append(notification)
def broadcast(self, message: str): """Send message through all notification channels""" for notification in self.notifications: notification.send(message) # Polymorphism - each type handles differently
# Usageservice = NotificationService()service.add_notification(SMSNotification("+1234567890"))service.add_notification(PushNotification("device-123"))
# All notifications work the same wayservice.broadcast("Your order has been shipped!")// Base notification classpublic abstract class Notification { public abstract boolean send(String message);}
public class EmailNotification extends Notification { private String recipient;
public EmailNotification(String recipient) { this.recipient = recipient; }
@Override public boolean send(String message) { // Email-specific implementation System.out.println("Sending email to " + recipient + ": " + message); return true; }}
public class SMSNotification extends Notification { private String phoneNumber;
public SMSNotification(String phoneNumber) { this.phoneNumber = phoneNumber; }
@Override public boolean send(String message) { // SMS-specific implementation String truncated = message.length() > 50 ? message.substring(0, 50) + "..." : message; System.out.println("Sending SMS to " + phoneNumber + ": " + truncated); return true; }}
public class PushNotification extends Notification { private String deviceId;
public PushNotification(String deviceId) { this.deviceId = deviceId; }
@Override public boolean send(String message) { // Push notification-specific implementation System.out.println("Sending push to device " + deviceId + ": " + message); return true; }}
// Service that can use any notification typepublic class NotificationService { private java.util.List<Notification> notifications;
public NotificationService() { this.notifications = new java.util.ArrayList<>(); }
public void addNotification(Notification notification) { // Add any notification type notifications.add(notification); }
public void broadcast(String message) { // Send message through all notification channels for (Notification notification : notifications) { notification.send(message); // Polymorphism - each type handles differently } }}
// Usagepublic class Main { public static void main(String[] args) { NotificationService service = new NotificationService(); service.addNotification(new SMSNotification("+1234567890")); service.addNotification(new PushNotification("device-123"));
// All notifications work the same way service.broadcast("Your order has been shipped!"); }}Benefits of Polymorphism
Section titled “Benefits of Polymorphism”- Code Reusability - Write code once, use with multiple types
- Flexibility - Easy to add new types without changing existing code
- Maintainability - Changes to one type don’t affect others
- Simplicity - One interface for multiple implementations
- Extensibility - Easy to extend functionality
Key Takeaways
Section titled “Key Takeaways”Polymorphism is about flexibility - writing code that works with multiple types without knowing the specific type at compile time. It’s one of the most powerful features of object-oriented programming.