Abstract, Virtual, Interface in APEX – Part II

Hello Devs,
Today I would like to expand the Abstract, Virtual, Interface theme 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 pieces of data and behavior related to that data can be wrap into special bundles called object. Object is kind of abstraction defined by a programmer, which try to mimic some real subject.

Object-oriented programming has four pillars:

Abstraction

Every class you are developing, try to replicate real-word objects and their behaviors. Class is only abstract model with attributes and description 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 to "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 extend the existing class and add extra functionality into your child class.


Interface vs Abstract vs Virtual

Below you can find table, which contains 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? 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 contains abstract, virtual and implements 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 onw methods
e.g public virtual String getFullName(String firstName, String lastName);
Implement Parent Mehthods ✅ All interface’s methods needs to be implemented ✅ Only methods signed as abstract ❌ Cannot force child class to implements parent methods
Override Parent Methods ❌ Interface doesn’t contain logic to override ✅ Only methods signed as virtual ✅ Only methods signed as virtual
Has basic logic? ❌ Interface contains only method signature ✅ Methods signed as virual and inner methods can contains basic logic ✅ Methods signed as virual and inner methods can contains basic logic
Can be initialized directly? new MyInterface(); new MyAbstractClass(); ✅ new MyVirtualClass();
Pros ✅ New layer of abstraction.
✅ Interface can be treated as a new data type.
✅ Logic behind interface can be change without changes in related classes.
✅ We program to interfaces, not implementations. Code doesn’t have reference to specific classes.
✅ Child class is forced to implement interface’s method (Guarantee specific logic).
✅ Child class can implement many interfaces.
✅ Abstract class can store common logic (avoid redundancy)
✅ Child class can be forced to implement some (signed as abstract) methods.
✅ Child class can override some (singed as virtual) methods.
✅ Basic logic can be provided by abstract class inner methods. (private, protected, public)
✅ Easy to update common code for all child method. Just update code in abstract class.
✅ virtual class can be treated as a base class with base logic, which shoul be accessed by all child classes.
✅ Virutal class can be initialized direct
✅ Child class can override some (signed as virtual methods)
✅ Easy to update common code for all child method. Just update code in virtual class.
Basic logic can be provided by abstract class inner methods. (private, protected, public)
Cons ❌ Hard to add or change interface’s methods, because all classes that implements interface needs to be change 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.
❌ Child class can be only extended by one parent class.
❌ Cannot force child class to implement parent method (abstract cannot be used)
❌ Child class can be only extended by one parent class.
Goal To create new layer of abstraction To provide kind of "partial" class with common logic To provide kind of "full" class with base logic

Resources

5 1 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Дмитрий Попов
Дмитрий Попов
12 days ago

Thank you! Good explaining!

Close Menu
1
0
Would love your thoughts, please comment.x
()
x