What is polymorphism?
- Last Updated: January 16, 2024
- 1 minute read
- OpenEdge
- Version 12.8
- Documentation
Polymorphism is a powerful object-oriented feature that reduces the amount of ABL code you need to write.
To use polymorphism, you must write your classes to use either inheritance or interfaces. If you use inheritance, the derived classes must all define the same method defined in the super class. Interfaces already provide this capability as all implementations of an interface class must implement the same method.
Polymorphism allows you to write ABL code to access an instance of a super class or interface class (without worrying about particulars of the derived or implemented classes or methods), but at runtime, ABL dynamically calls the method for the derived or implemented class.
Here is an example with interfaces. Suppose you have defined the ICustomerInvoice interface class that specifies that a
method named SendInvoice() must be implemented. You
define the RetailCustomerInvoice and EnterpriseCustomerInvoice classes to implement
ICustomerInvoice. The SendInvoice() method in RetailCustomerInvoice uses a home address format for
sending the invoice. The SendInvoice() method in
EnterpriseCustomerInvoice uses a business address
format with a PO number for sending the invoice. Polymorphism enables you to write code
to process all invoices, regardless of whether they are for retail or enterprise
customers. The code you write calls the SendInvoice()
method for the instance of the ICustomerInvoice
interface class. It does not have to determine whether an instance is for a retail
customer or enterprise customer. ABL dynamically chooses the correct method to call at
runtime.