Apex is an object-oriented programming language that allows developers to execute flow, transaction, and control statements on the Salesforce platform.
Devs who are used to Java or .NET development will find that programming in Apex is fairly straightforward. Also, beginners who have never coded before might be surprised by 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.
- It is not mandatory but virtual methods can be overwritten.
- The abstract class allows us an easy way to update our logic for all child classes.
Limitations
- Cannot be initialize directly:
new TestAbstractClass() - The methods in the class can only be used if the class is extended by another class.
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.
- Virtual methods can be overridden. As a result, overriding a virtual method allows us to provide a different implementation for an existing method.
- The virtual class can be initialized directly. For example, 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.
- Possibility to initialize parent class with new. For example, the class needs to be used directly.
- We have a basic logic, but some methods can be overridden by the child class (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.
- 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
Leave a Reply