Introduction
Polymorphism is one of the fundamental principles of Object-Oriented Programming (OOP) that allows objects to take on many forms.
In Salesforce Apex, polymorphism plays a crucial role in writing flexible, reusable, and maintainable code.
In this blog post, we’ll break down what polymorphism is, how it works in Salesforce Apex, and how it ties into OOP principles. Let’s get started!
What is Polymorphism?
Polymorphism is a Greek word that means “many forms.” In programming, it refers to the ability of an object, method, or property to take on many forms. In simpler terms, it allows you to use a single interface (like a method or class) to represent different types of objects or behaviors.
Polymorphism is one of the four core principles of Object-Oriented Programming (OOP). The other three are:
Encapsulation: Hiding the internal details of how an object works.
Inheritance: Creating new classes based on existing ones.
Abstraction: Simplifying complex systems by modeling classes appropriate to the problem.
Polymorphism works hand-in-hand with inheritance and abstraction to make your code more flexible and reusable.
Types of Polymorphism in Apex
In Salesforce Apex, there are two main types of polymorphism:
Compile-Time Polymorphism (Method Overloading)
Run-Time Polymorphism (Method Overriding)
Let’s explore each type with examples.
1. Compile-Time Polymorphism (Method Overloading)
Method overloading happens when you have multiple methods in the same class with the same name but different parameters.
The correct method to call is determined at compile time based on the arguments you pass.
Example:
public class Calculator {
// Method to add two integers
public Integer add(Integer a, Integer b) {
return a + b;
}
// Overloaded method to add three integers
public Integer add(Integer a, Integer b, Integer c) {
return a + b + c;
}
}
In this example, the add
 method is overloaded. Depending on whether you pass two or three integers, the correct method is called:
Calculator calc = new Calculator();
Integer result1 = calc.add(2, 3); // Calls the first method
Integer result2 = calc.add(2, 3, 4); // Calls the second method
2. Run-Time Polymorphism (Method Overriding)
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its parent class.
The method to call is determined at runtime based on the object’s type.
// Parent class
public class Animal {
public void makeSound() {
System.debug('Animal makes a sound');
}
}
// Subclass overriding the method
public class Dog extends Animal {
public override void makeSound() {
System.debug('Dog barks');
}
}
// Another subclass
public class Cat extends Animal {
public override void makeSound() {
System.debug('Cat meows');
}
}
Here, the makeSound
 method is overridden in the Dog
 and Cat
 classes. When you call the method, the behavior depends on the object’s type:
Animal myAnimal = new Animal();
myAnimal.makeSound(); // Output: Animal makes a sound
Animal myDog = new Dog();
myDog.makeSound(); // Output: Dog barks
Animal myCat = new Cat();
myCat.makeSound(); // Output: Cat meows
Notice how the same method (makeSound
) behaves differently depending on whether it’s called on an Animal
, Dog
, or Cat
 object. This is run-time polymorphism in action!
Real-World Example in Salesforce
Imagine you’re building a Salesforce application to manage different types of notifications (email, SMS, and Slack). You can use polymorphism to handle all notifications in a unified way.
// Parent class
public class Notification {
public virtual void send() {
System.debug('Sending a generic notification');
}
}
// Subclass for Email notifications
public class EmailNotification extends Notification {
public override void send() {
System.debug('Sending an email notification');
}
}
// Subclass for SMS notifications
public class SMSNotification extends Notification {
public override void send() {
System.debug('Sending an SMS notification');
}
}
// Subclass for Slack notifications
public class SlackNotification extends Notification {
public override void send() {
System.debug('Sending a Slack notification');
}
}
Now, you can send notifications without worrying about the specific type:
Notification email = new EmailNotification();
Notification sms = new SMSNotification();
Notification slack = new SlackNotification();
email.send(); // Output: Sending an email notification
sms.send(); // Output: Sending an SMS notification
slack.send(); // Output: Sending a Slack notification
Benefits of Polymorphism in Apex
Polymorphism makes your code:
Reusable: You can write generic code that works with different types of objects.
Flexible: You can easily extend your code by adding new subclasses without changing existing code.
Maintainable: It reduces code duplication and makes your code easier to understand.
If you’re eager to learn more OOP concept, then do check out the below posts.
Conclusion
Polymorphism is a powerful concept in Salesforce Apex that allows you to write flexible, reusable, and maintainable code.
By understanding and applying method overloading (compile-time polymorphism) and method overriding (run-time polymorphism), you can take full advantage of OOP principles in your Apex development.
Whether you’re building simple calculators or complex notification systems, polymorphism will help you write cleaner and more efficient code.Â
So, the next time you see a method behaving differently in different contexts, you’ll know it’s polymorphism at work!