Polymorphism In Apex

One name many forms is known as polymorphism or in other words you can say many forms are known as polymorphism.

Real life example of polymorphism: lets take an example, in a college a Director wants to call all HODs for annual meeting so he will just simply publish into notice board that all ALLHOD’S are invited which means one name i.e. ALLHOD’S pointing to all HOD of the college of different departments so by which we can say one name many forms known as polymorphism.

Polymorphism can achieve by two ways:

  • Method Overloading in Apex
  • Method Overriding in Apex

Method Overloading in Apex:

If a class have multiple methods with same name but different parameters (either different length of the arguments or different data types of the arguments), it is known as Method Overloading.

If we have to perform only one operation, with the same name of the methods increases the readability of the program as well as its very logical too.

Let’s suppose if we want to perform addition of the given numbers but there can be any number in arguments as below:

Example of method overloading with different data types of the arguments with same method name like sum.

public class MethodOverloadDemo{
  public void sum(Integer no1, Integer no2){
    System.debug(‘Sum of 2 Integer is : ‘+ (no1+no2));
  }
  
public void sum(Decimal no1, Decimal no2){

    System.debug(‘Sum of 2 Integer is : ‘+(no1+no2));
  }
}

apex-class

To run : Create an object and call methods

MethodOverloadDemo mo = new MethodOverloadDemo();
mo.sum(10,20);
mo.sum(10.5, 2.5);

enter-apex

Method Overriding In Apex

If a subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Apex.

In other words, we can say If a subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

Note: Always make sure to achieve method overriding the scope must be of parent and child class relationship and you must have same name method needs to declare into child class with same signature of the method from Parent class to achieve method overriding in Apex.

Usage of Apex Method Overriding

Method overriding is used to provide specific implementation of a method that is already provided by its Parent/Super class. Method overriding is used to achieve runtime polymorphism.

Rules for Apex Method Overriding

  • Method name must be same as in the parent class.
  • Method name must have same parameter as in the parent class’s method.
  • Must be inheritance (IS-A relationship).

Syntax for Method Override

public virtual class Parent {
  public virtual void display() {
    System.debug(‘This is Parent’);
  }
}

apex-virtual-class-parent

public class Child extends Parent{
  public override void display(){
    System.debug(‘This is form Child’);
  }
}

apex-child-extend-parent

Child ch = new Child();
//OR
Parent ch = new Child();
ch.display();

apex-code1

Output :
output

Subscribe Now