Modifiers in Apex

Concept of Virtual and Abstract Modifiers in Apex | Salesforce Developer Guide

Implementing the Virtual or Abstract modifiers in the class makes it inheritable by any other class present in Salesforce. Let us understand each of them by example.

Consider two classes, parentClass and childClass. Here is the code block:

public virtual class parentClass {   
    public virtual integer doDivide(integer x, integer y){
        return x/y;
    }
    public static integer doSum(integer x, integer y){
        return x+y;
    }
    public integer doDifference(integer x, integer y){
        return x-y;
    }
}
public class childClass extends parentClass {
    public override integer DoMultiply(integer x, integer y){
        return x*y; 
    }
    public override integer doDivide(integer x, integer y){
        return x/y +100;
    }   	
}

dont miss out iconDon't forget to check out: Future Methods in Apex | The Salesforce Developer Guide

As shown, the childClass can extend the parentClass only when the parentClass is defined as either virtual or abstract. Once the childClass extends the parentClass, all the methods defined in the parentClass can be called from the childClass. For example, the doDIfference method in parentClass can simply be called from childClass as:

childClass child = new childClass();
child.doDifference(10,2);

Result: 8

However, sometimes, we may not want to use the same method body as defined in the parentClass and instead, we may require some other code within the same method i.e. we want to override the existing code in the method. For this, we need to first make the method in the parentClass as virtual (or abstract) and then in the childClass we need to use the override keyword on the method definition. For example: let's look at the doDivide method in both classes. Since we are overriding this method in the childClass, we have inserted the override keyword in the method definition. Also, in the parentClass, this method is defined as virtual to enable it to be used in childClass.

Note: for a method to be virtual, the class has to be either virtual or abstract.

Similar is the case for the abstract class with a minor difference: the abstract methods have only declarations and no body.  For example, let's look at the code:

public abstract class parentClass {   
    public virtual integer doDivide(integer x, integer y){
        return x/y;
    }
    public static integer doSum(integer x, integer y){
        return x+y;
    }
    public abstract integer doDifference(integer x, integer y);
}
public class childClass extends parentClass {
    public integer DoMultiply(integer x, integer y){
        return x*y; 
    }
    public integer doNothing(integer x, integer y){
        return 0;
    }
    public override integer doDifference(integer x, integer y){
        return (x-y) +10;
    }
}

The doDifference method is an abstract method and has only the definition in the parentClass and no body. Also, if the childClass is extendig an abstract parentClass, then the method corresponding to the abstract method in parentClass has to be implemented in the childClass.

Difference between Virtual and Abstract modifiers:

While both the virtual and abstract modifiers makes the class extensible, they have few differences :

  • The abstract methods can only be defined in an abstract class, whereas virtual methods can be defined in virtual as well as abstract class.
  • The abstract method in the parentClass has only the signatures declared and not the body whereas the vitrual method has defjnition as well as body within the method.
  • Vitual method in the parentClass may or may not be overridden in childClass, whereas the abstract method in parentClass has to be overridden and used within childClass.

dont miss out iconCheck out another amazing blog by Anurag here: Lightning Data Service (LDS) in Salesforce

Points:

  1. static methods in the parentClass cannot be overridden in the childClass. They can simply be called from the childClass in a usual way.
  2. the virtual and abstract methods in the parentClass cannot be static. Also the methods overriding them in the childClass cannot be static.
  3. the method defined as virtual or abstract should never be private.

Responses

Popular Salesforce Blogs