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.
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
- Our classes can have common methods with similar logic.
- You can initialize the parent class with
newwhen the class needs to be used directly. - 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
Nice, well explained