Abstract, Virtual, Interface in APEX – Part II

Hello Devs,
Today I would like to expand the subject Abstract, Virtual, Interface which was described in Abstract, Virtual, Interface in APEX – Part I
Let’s begin.

Object-oriented programming

Object-oriented programming (OOP) – is a paradigm based on the concept that every piece of data and behavior related to that data can be wrap into special bundles called objects. The object is a kind of abstraction defined by a programmer, which tries to mimic some real subject.

Object-oriented programming has four pillars:

Abstraction

Every class you are developing, try to replicate real-world objects and their behaviors. Class is only an abstract model with attributes and descriptions of behavior (methods).

Encapsulation

Encapsulation is the ability of an object to hide parts of its state and behaviors from other objects, exposing only a limited interface to the rest of the program.

Encapsulation is provided by Interfaces and abstract classes/methods of most programming languages.

Polymorphism

Polymorphism is the ability of a program to detect the real class of an object and call its implementation even when its real type is unknown in the current context.

Your class can “pretend” that is something else.

Inheritance

Inheritance is the ability to build new classes on top of existing ones.

You can reuse existing code by extending the existing class and add extra functionality into your child’s class.


Interface vs Abstract vs Virtual

Below you can find a table, which contains a comparison between Interface, Abstract, and Virtual in Apex.

Feature Interface Abstract Virtual
Keyword interface
e.g public interface MyInterface {}
abstract
e.g public abstract class MyAbstractClass {}
virtual
e.g public virtual class MyVirtualClass {}
How to use it? By implements
e.g public class MyChildClass implements MyInterface {}
By extends
e.g public class MyChildClass extends MyAbstractClass {}
By extends
e.g public class MyChildClass extends MyVirtualClass {}
Type of Methods Only method signature
e.g String getFullName(String firstName, String lastName);
Can contain abstract, virtual and implements its own methods.
e.g abstract String getFullName(String firstName, String lastName);
public virtual String getFullName(String firstName, String lastName);
Can contains only virual and implements own methods
e.g 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? ❌  new MyInterface(); ❌  new MyAbstractClass(); ✅  new MyVirtualClass();
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 be only extended by one parent class.
❌  Cannot force child class to implement parent method (abstract cannot be used)
❌  A child class can be only extended by one parent class.
Goal To create a new layer of abstraction. To provide a kind of “partial” class with common logic. To provide a kind of “full” class with base logic.

Resources

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Close Menu
0
Would love your thoughts, please comment.x
()
x