Difference between Abstract, Virtual and Interface in APEX

Apex is an object-oriented programming language that allows developers to execute flow, transaction, and control statements on the Salesforce platform.

Developers familiar with Java or .NET development will find programming in Apex fairly straightforward. Beginners with no coding experience might find it surprising how easy it is to access and manipulate data with Apex.

In this article, we will give a brief description of Abstract, Virtual, and Interface in Apex.

Abstraction

  • In Apex, developers define data abstraction as the process of reducing the object to its essence, exposing only the necessary characteristics to the users. Abstraction defines an object in terms of its properties, behaviors, and interfaces.

Encapsulation

In Java, developers use encapsulation to wrap data (variables) and the code acting on the data (methods) together as a single unit. Encapsulation hides the variables of a class from other classes, and developers can access them only through the methods of their current class.

Polymorphism

A class that extends another class inherits all the methods and properties of the extended class. Additionally, the extending class can override the existing virtual methods by using the override keyword in the method definition. Therefore, overriding a virtual method allows you to provide a different implementation for an existing method. This means that the behavior of a particular method is different based on the object you’re calling it on. We refer to this as polymorphism.

Inheritance

Inheritance is the ability to build new classes on top of existing ones. With that said, we can reuse existing code by extending the existing class and add extra functionality into your child’s class.


Interface vs Abstract vs Virtual

Below is a table, which contains a comparison between Interface, Abstract, and Virtual in Apex.

Feature Interface Abstract Virtual
Keyword interface
public interface MyInterface {}
abstract
public abstract class MyAbstractClass {}
virtual
public virtual class MyVirtualClass {}
How to use it? By implements
public class MyChildClass implements MyInterface {}
By extends
public class MyChildClass extends MyAbstractClass {}
By extends
public class MyChildClass extends MyVirtualClass {}
Type of Methods Only method signature
String getFullName(String firstName, String lastName);
Can contain abstract, virtual and implements its own methods.
abstract String getFullName(String firstName, String lastName);
public virtual String getFullName(String firstName, String lastName);
Can contains only virual and implements own methods
public virtual String getFullName(String firstName, String lastName);
Implement Parent Methods ✅  All interface methods need to be implemented. ✅  Only methods signed as abstract ❌  Cannot force child class to implements parent methods.
Override Parent Methods ❌  The interface doesn’t contain logic to override. ✅  Only methods signed as virtual ✅  Only methods signed as virtual
Has basic logic? ❌  The interface contains only the method signature. ✅  Methods signed as virtual and inner methods can contain basic logic. ✅  Methods signed as virual and inner methods can contain basic logic.
Can be initialized directly? ❌  No, new MyInterface(); ❌  new MyAbstractClass(); ✅  new MyVirtualClass();

Interface vs Abstract Vs Virtual Pros, Cons and Goals

Feature Interface Abstract Virtual
Pros ✅  A new layer of abstraction.
✅  The interface can be treated as a new data type.
✅  The logic behind the interface can be changed without changes in related classes.
✅  We program to interfaces, not code implementations. Code doesn’t have references to specific classes.
✅  The child class is forced to implement the interface’s method (Guarantee specific logic).
✅  A child class can implement many interfaces.
✅  An abstract class can store common logic (avoid redundancy).
✅  A child class can be forced to implement some (signed as abstract) methods.
✅  A child class can override methods (singed as virtual).
✅  Basic logic can be provided by abstract class inner methods. (private, protected, public)
✅  Easy to update common code for all child methods. Just update code in abstract class.
✅  The virtual class can be treated as a base class with base logic, which should be accessed by all child classes.
✅  The virtual class can be initialized directly.
✅  A child class can override methods (signed as virtual).
✅  Easy to update common code for all child methods. Just update code in a virtual class.
Basic logic can be provided by abstract class inner methods (private, protected, public).
Cons ❌  Hard to add or changed interface methods, because all classes that implement interface needs to be changed as well.
❌  Apex class needs to implement all interface methods even if it’s not relevant.
❌  Cannot be initialized directly.
❌  Apex class needs to implement all abstract methods even if it’s not relevant.
❌ A child class can only extend one parent class.

❌  Cannot force child class to implement parent method (abstract cannot be used)
❌ A child class can only extend one parent class.
Goal Creation of a new layer of Abstraction. Provide a kind of “partial” class with common logic. To provide a kind of “full” class with base logic.

Interface

Use Cases
  1. When we need to guarantee a specific logic. For example, we can provide an interface to another team before it starts working on an implementation for particular methods. Therefore, they won’t have any blockers and don’t need to wait for our code modifications.
  2. When we need to create new data types with some set of methods, but with different implementations.
Description

Interface in Apex is a collection of unimplemented methods and it contains the method signature, while the body of each method is empty. Therefore, when we use Interface in Apex, another Apex class must implement it by providing the body for all the methods contained in Apex interfaces.

Good examples are Database.Batchable, Queueable, Schedulable interfaces. This way Salesforce forces us to implement specific methods.

Benefits
  • We program to interfaces, not implementations!
  • The class can implement a few interfaces! As result, it allows our class to have different behaviors.
  • Code is easier to change when it depends only on interfaces. Doesn’t have reference to specific classes, but to interface.
  • Good choice if the API will not change for a while.
Limitations
  • Adding a new method to an interface can be difficult because we need to change all the classes that implement that interface.
Code Structure
// Interface
public interface Worker{
   // Method's signature
   DoubleincreaseWorkerSallary(Double currentSalarry);
}

//Company A
public class CompanyAWorker implements Worker{
   public Double DoubleincreaseWorkerSallary(Double currentSalarry) {
      return currentSalarry * 2.1;
   }
}

//Company B
public class CompanyBWorker implements Worker {
   public Double increaseWorkerSallary(Double currentSalarry) {

      return currentSalarry * 1.2;
   }
}

// Because each class implements Worker interface, we can do something like that:
Worker workerA= new CompanyAWorker();
Worker workerB= new CompanyBWorker();

workerA.increaseEmployeeSallary(1000.0); //2100
workerB.increaseEmployeeSallary(1000.0); //1200

Abstract

Use Cases
  1. Classes that contain common methods with similar code logic.
  2. You can add new methods in the future.
  3. Sometimes there might be some logic that must be class-specific. For example, Company 1 and Company 2 have the same payment() implementation, but they are different for IncreaseSalary(). In this case, we can mark IncreaseSalary() method as abstract.
Definition

When creating an abstract class, you must use the abstract definition modifier. An abstract class can contain methods with only a signature, defined as abstract. The child class must implement all methods declared as abstract!

We can have different signatures for our methods:

    • private – child class doesn’t have access to method signed as private.
    • protected – child class has access to parent class method, but any other class doesn’t have access.
    • public – In other words, making the method accessible by any other class.

An abstract class can also include other methods, which contain logic. If we want to allow child class access to those methods we need to use protected or public keywords.

Benefits
  • Can contain both virtual and abstract methods.
  • You don’t have to make virtual methods mandatory, but you can overwrite them.
  • The abstract class allows us an easy way to update our logic for all child classes.
Limitations
  • Cannot be initialize directly: new TestAbstractClass()
  • You can only use the methods in the class if another class extends it.
Code Structure
//Abstract Parent class
public abstract class TestAbstractClass {

    protected String test1;
    public String test2;

    TestAbstractClass() {}

    TestAbstractClass(String test1, String test2) {
       this.test1 = test1;
       this.test2 = test2;
    }

    abstract public String myAbstractMethod();

    public virtual String myVirtualMethod() {
       return 'virtual method from abstract class';
    }

    private String myPrivateMethod() {
       return 'private method from abstract class';
    }

    protected String myProtectedMethod() {
       return 'protected method from abstract class';
    }

    public String myPublicMethod() {
       return 'public method from abstract class';
    }
}
//Child class
public class ExampleClass extends TestAbstractClass {

   ExampleClass(String test1, String test2) {
     //Abstract class constructor can use super. 
     //Remember! Abstract class cannot be initialized, 
     //so this is the only scenario when we can use abstract class constructor!
      super(test1, test2);
   }

   public String myAbstractMethod() {
      return 'Abstract method implemented in child class!';
   }

   public override String myVirtualMethod() {
      return 'Virtual method overridden in child class!';
   }   

   public String invokeMethodFromAbstractClass() {
      String methods = '';
      methods += myPrivateMethod();//error, private method is not accessible in child class
      methods += myProtectedMethod();
      methods += myPublicMethod();

      return methods; //protected method from abstract class public method from abstract class
   }
}
ExampleClass example = new ExampleClass('Hello', 'World');
System.debug(example.test1); //error. Property test1 can be accessed only in child class.
System.debug(example.test2); //output: 'World';
System.debug(example.myAbstractMethod()); //output: 'Abstract method implemented in child class!'
System.debug(example.myVirtualMethod()); //output: 'Virtual method overridden in child class!'
System.debug(example.myPrivateMethod()); //error. It can be used only in parent class
System.debug(example.myProtectedMethod()); //error. It can be used only in parent and child classes
System.debug(example.myPublicMethod()); //output: 'public method from abstract class'

TestAbstractClass abstractClass = new TestAbstractClass();//error. Abstract class cannot be initialized

Virtual

Description

The virtual definition modifier declares that this class allows extension and overrides. As result, a class that extends another class inherits all the methods and properties of the extended class. A Virtual class is some kind of “full” class. Therefore, when creating a virtual class, we need to use the virtual definition modifier.

Benefits
  • Extend a class to provide more sophisticated behavior.
  • Override virtual methods to provide a different implementation for an existing method.
  • Initialize the virtual class directly, e.g., new TestVirtualClass();
  • We can use only virtual methods.
Limitations
  • Virtual classes can only extend one class, but they can implement more than one interface!
Use Cases
  1. Our classes can have common methods with similar logic.
  2. You can initialize the parent class with new when the class needs to be used directly.
  3. The logic is basic, but the child class can override some methods (virtual methods).
Code Structure
//Parent Virtual Class
private virtual class TestVirtualClass {
   
   protected String test1;
   public String test2;

   TestVirtualClass() {}

   TestVitrualClass(Strint test1, String test2) {
       this.test1 = test1;
       this.test2 = test2;
   }

   public virtual String myVirtualMethod() {
      return 'virtual method from virtual class';
   }

   private String myPrivateMethod() {
      return 'private method from virtual class';
   }

   protected String myProtectedMethod() {
      return 'protected method from virtual class';
   }

   public String myPublicMethod() {
      return 'public method from virtual class';
   }
}
//Child class
public class ExampleClass extends TestVirtualClass {
   
   ExampleClass(String test1, String test2) {
      //Virtual class constructor can use super. 
      super(test1, test2);
   }

   public override String myVirtualMethod() {
      return 'Virtual method overridden in child class!';
   }   

   public String invokeMethodFromAbstractClass() {
      String methods = '';
      methods += myPrivateMethod();//error, private method is not accessible in child class
      methods += myProtectedMethod()
      methods += myPublicMethod();

      return methods; //protected method from abstract class public method from abstract class
   }
}
ExampleClass example = new ExampleClass('Hello', 'World');
System.debug(example.test1); //error. Property test1 can be accessed only in child class.
System.debug(example.test2); //output: 'World';
System.debug(example.myVirtualMethod()); //output: 'Virtual method overridden in child class!'
System.debug(example.myPrivateMethod()); //error. It can be used only in parent class
System.debug(example.myProtectedMethod()); //error. It can be used only in parent and child classes
System.debug(example.myPublicMethod()); //output: 'public method from virtual class'

TestVirtualClass virtualClass = new TestVirtualClass();
System.debug(virtualClass.myVirtualMethod()); //output: 'virtual method from virtual class' 
System.debug(virtualClass.myPrivateMethod()); ////error. It can be used only in parent class
System.debug(virtualClass.myProtectedMethod()); //error. It can be used only in parent and child classes
System.debug(virtualClass.myPublicMethod()); //output: public method from virtual class'

Conclusion

Interface, Abstract, and Virtual classes are different methods to make our code more generic, reusable, and clear to other developers.

  • Interface provides another layer of abstraction, therefore it makes it easier to change our implementation.
  • Additionally, Abstract class allows us to force the developer that is using the class, to implement some methods.
  • Virtual is the lightest solution, which gives us the possibility to override some method provided by the parent class.
Want to learn more?

Learn about Salesforce DX commands for removing old org from list
Read more about Apex Class Definition

Share this article...

As Managing Partner and CTO at codeSTREETS, I help global businesses scale through robust Salesforce architecture and custom software solutions. With 10+ years of experience in enterprise transformation, I lead teams to deliver results-driven, future-ready digital platforms. My expertise spans cloud architecture, product innovation, and mentorship, earning recognition as a speaker and community leader. Let’s connect if you’re seeking tailored solutions, seamless implementation, or strategic partnerships to accelerate your business growth.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *