Today I would like to expand the Abstract, Virtual, Interface theme which was described in Abstract, Virtual, Interface in APEX – Part I
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:
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 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 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 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.
|How to use?||By
|Type of Methods||Only method signature e.g
||Can contains only
|Implement Parent Mehthods||✅ All interface’s methods needs to be implemented||✅ Only methods signed as
||❌ Cannot force child class to implements parent methods|
|Override Parent Methods||❌ Interface doesn’t contain logic to override||✅ Only methods signed as
||✅ Only methods signed as
|Has basic logic?||❌ Interface contains only method signature||✅ Methods signed as
||✅ Methods signed as
|Can be initialized directly?||❌
||✅ 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
||✅ 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
|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
||❌ Cannot force child class to implement parent method (
|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|