Polymorphism in Salesforce Apex (With Real-Time Examples & Easy Explanation)

Polymorphism is one of the most important concepts in Salesforce Apex and object-oriented programming (OOP).

If you are learning Apex or preparing for interviews, understanding polymorphism is very important because it helps you write flexible, reusable, and scalable code.

In simple words, polymorphism means “many forms.” It allows the same method or function to behave differently depending on the object that is calling it.

In Salesforce Apex, polymorphism works similarly to languages like Java, where a single method name can perform different operations based on context.

In this Salesforce tutorial, we will learn about polymorphism in Salesforce Apex. Next, I will explain how to achieve polymorphism in Salesforce Apex, providing examples and a step-by-step explanation.

What is Polymorphism in Salesforce Apex?

In Salesforce Apex, polymorphism is a core OOP concept. Polymorphism means that one method can perform multiple tasks depending on the situation.

Instead of creating different method names, we use the same method name but change its behavior.

The term polymorphism originates from the Greek words “poly,” meaning many, and “morph,” meaning form, which translates to “many forms,” referring to the ability to be represented in multiple forms. So, Apex has two types of polymorphism.

In Apex, if you want to modify the parent class method in the child class, you need to use the override keyword in the child class.

The override keyword informs Apex that a new version of the same method is available in the child class. We can also access the parent class method using the Super keyword.

Overriding occurs when two or more methods with the same name and parameters are defined in different classes, each with a different implementation. It is also called dynamic or run-time polymorphism.

Types of Polymorphism in Salesforce Apex

Two types of polymorphism allow multiple methods with the same name.

  • Compile-Time Polymorphism: This is implemented using method overloading.
  • Run-time Polymorphism: This is represented by an overriding method.

Why Polymorphism is Important in Salesforce?

Polymorphism is widely used in Salesforce because:

  • Helps in code reusability
  • Makes code easy to maintain
  • Supports scalable applications
  • Used in Triggers, Classes, and Integrations

In real Salesforce projects:

  • You may use the same method for different objects (Account, Contact, Lead)
  • But each object behaves differently

Implement Polymorphism in Salesforce Apex

In the steps below, I will explain how to implement polymorphism using method overloading and overriding in Salesforce Apex. We can achieve polymorphism in Salesforce Apex by implementing inheritance and interfaces.

Compile-Time Polymorphism (Overloading Method) in Salesforce Apex

Method overloading is a form of compile-time polymorphism that allows you to define multiple methods with the same name but different parameter lists within a single class. As we create the same process for this, we can call it a type of polymorphism.

Below, I created the Apex class and then defined a method named subject(). Overloading allows us to use this method repeatedly, but the parameters must differ.

Here, you can see that I also created multiple methods with the same name, but with different parameters. By using these methods, we can implement different operations for the same function.

public class RecordList {
    public void DisplayRecord() {
        List<Account> accounts = [ SELECT Id, Name, Rating FROM Account LIMIT 3];
           for (Account acc: accounts) {
            System.debug ('Account Name:' + acc.Name + 'Account ID:' +acc.Id + 'Ratings:' + acc.Rating);
        }
    }
    public void DisplayRecord(List<Contact> contacts) {
        contacts = [ SELECT Id, Name, Account.Name FROM Contact WHERE Account.Name = 'United Oil & Gas Corp.' Limit 3 ];
           for ( Contact con: contacts ) {
            System.debug ('Contact Name:' + con.Name);
        }
    }
    public void DisplayRecord(List<Lead> leadList) {
        leadList = [ SELECT Id, Name, LeadSource FROM Lead LIMIT 3 ];
           for (Lead l : leadList) {
            System.debug( 'Lead Name' + l.Name + 'Lead Source:' +l.LeadSource);
        }
    }
}

As you execute the above Apex class code, you can see that the method is the same, but the end result is different. We can achieve this by using method overloading in polymorphism.

Polymorphism in Salesforce Apex

In this way, we can create an overloading method in Salesforce Apex.

Run-Time Polymorphism (Overriding Method) in Salesforce Apex

Method overriding is a type of run-time polymorphism that allows you to provide a specific implementation for a method in a child class that is already defined with the same name and parameter list in its parent class.

As we create the same method for this, we can call it a type of polymorphism. In method overriding, we inherit from the parent class, and in the child class we can override the same method.

We need to use inheritance or an interface in Salesforce Apex to achieve polymorphism for method overriding.

When overriding a method, we need to create a method that matches the parent class’s declaration, using the same parameter list.

When we create an override, the method we override must be declared virtual in the parent class. Then, implement the method according to your requirements. Then, I created a map collection in the Salesforce database to store the products.

public virtual class Products {
    public virtual void updateProducts() {
        Map<Integer,String> productList = new Map<Integer,String>();
        productList.put (101,'GenWatt Diesel 1000kW');
        productList.put (102,'SLA: Bronze');
        productList.put (103,'SLA: Platinum');
        productList.put (104,'Monitor c32220');

        System.debug (' Display List of Created Products : '+productList);
    }
}

Then, create another class that extends the parent class. In this class, we can override the method that we declared in the parent class. We need to add an override keyword before declaring the method.

The use of creating duplicate methods is to implement the functionality of this method rather than that of the parent class.

public class AddProducts extends Products{
    public override void updateProducts() {
        Map<Integer,String> productList = new Map<Integer,String>();
        productList.put (001,'Distance Bolt');
        productList.put (002,'Hexert Threaded Inser');
        productList.put (003,'Installation: Industrial - High');

        System.debug (' Display List of Updated Products : '+productList);
    }
}

Again, I created another class and used the same method here. We can also implement the existing method’s functionality.

public class NewProducts extends Products {
    public override void updateProducts() {
        List<String> ProductList = new List<String>();
        ProductList.add ('Vaccume Cleaner');
        ProductList.add ('leaser Cutter');
        ProductList.add ('Drilling Mechines');
        ProductList.add ('Electric Generator');

        System.debug (' Display List of Updated Products : '+ProductList);
    }
}

To execute the override method, we need to create an object of the class that contains the method we want to override. You can also execute all methods and call the method using an object instance.

Overriding in Salesforce Apex

In this way, we can create method overriding in Salesforce Apex polymorphism and implement the same method in the child class.

Method Overloading vs Method Overriding in Salesforce Apex

FeatureMethod OverloadingMethod Overriding
TypeCompile-timeRun-time
ClassSame classParent & Child class
ParametersDifferentSame
BehaviorBased on inputBased on the object

Conclusion

Polymorphism is a powerful concept in Salesforce Apex that allows a single method to behave in multiple ways. It helps developers write clean, flexible, and reusable code.

By understanding method overloading, method overriding, and polymorphic relationships, you can build scalable and efficient Salesforce applications.

If you want to become a strong Salesforce developer, mastering polymorphism is essential, as it is widely used in real-world projects and interviews.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.