Polymorphism

ICT2622 - Object-Oriented Analysis · Object-Oriented Concepts

Polymorphism

Polymorphism is a key concept in object-oriented programming (OOP). It allows methods to do different things based on the object that it is acting upon. In simpler terms, polymorphism enables one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation.

Types of Polymorphism

There are two main types of polymorphism in object-oriented programming: compile-time polymorphism and runtime polymorphism.

Compile-Time Polymorphism

Compile-time polymorphism is achieved through method overloading and operator overloading.

Method Overloading

Method overloading occurs when two or more methods in the same class have the same name but different parameters (different type or number of parameters). The correct method is selected at compile time based on the method signature.

class MathOperations {
public:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
MathOperations math;
int sumInt = math.add(5, 10);
double sumDouble = math.add(5.5, 10.5);

Remember: Method overloading is based on the method signature, which includes the method name and the parameter list.

Operator Overloading

Operator overloading allows you to define how operators (like +, -, *) behave for user-defined types (classes). This can make your classes easier to use and more intuitive.

class ComplexNumber {
public:
double real;
double imaginary;
ComplexNumber(double r, double i) : real(r), imaginary(i) {}
ComplexNumber operator+(const ComplexNumber& other) {
return ComplexNumber(real + other.real, imaginary + other.imaginary);
}
}
ComplexNumber c1(1.0, 2.0);
ComplexNumber c2(3.0, 4.0);
ComplexNumber c3 = c1 + c2;

Watch out: Ensure that operator overloading does not change the expected behaviour of the operator, as this can lead to confusion.

Runtime Polymorphism

Runtime polymorphism is achieved through method overriding, which occurs when a derived class provides a specific implementation of a method that is already defined in its base class. The specific method that is called is determined at runtime based on the object type.

Method Overriding

To override a method, the derived class must have the same method signature as the base class. In C++, the base class method should be marked as virtual to allow overriding.

class Animal {
public:
virtual void sound() {
cout << "Animal sound";
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Bark";
}
};
Animal* myDog = new Dog();
myDog->sound(); // Output: Bark

Remember: Use the virtual keyword in the base class to enable method overriding in derived classes.

Benefits of Polymorphism

Polymorphism enhances flexibility and maintainability in your code. Here are some key benefits:

  • Code Reusability: You can use the same interface for different data types.
  • Ease of Maintenance: Changes in the base class can automatically propagate to derived classes.
  • Improved Readability: Code that uses polymorphism is often easier to read and understand.

Real-World Example of Polymorphism

Consider a payment processing system. You may have different payment methods such as credit card, debit card, and PayPal. Each method may require different processing, but they all share a common interface.

class Payment {
public:
virtual void processPayment(double amount) = 0;
};
class CreditCardPayment : public Payment {
public:
void processPayment(double amount) override {
cout << "Processing credit card payment of " << amount << " rand";
}
};
class PayPalPayment : public Payment {
public:
void processPayment(double amount) override {
cout << "Processing PayPal payment of " << amount << " rand";
}
};
Payment* payment = new CreditCardPayment();
payment->processPayment(150.00); // Output: Processing credit card payment of 150.00 rand

Tip: Use polymorphism to create scalable and flexible systems, especially when you anticipate future changes or expansions.

Conclusion

Polymorphism is a powerful feature of object-oriented programming. It allows objects to be treated as instances of their parent class while still maintaining their own unique behaviours. This concept is crucial for creating flexible and maintainable software systems.

Check your understanding

  • What is the difference between compile-time and runtime polymorphism?
  • How does method overloading work in C++?
  • What is the purpose of the virtual keyword in a base class?
  • Provide an example of a real-world scenario where polymorphism could be applied.