In this Salesforce tutorial, we will learn about abstraction in Salesforce Apex. In that, I will explain what abstraction is, why we use it, and how to implement abstraction in Salesforce Apex.
What is Abstraction in Salesforce Apex?
In the Salesforce Apex programming language, abstraction is an object-oriented programming concept that hides unnecessary details and displays only the essential features of an object.
Hiding internal details and showing only the functionality is known as abstraction. For example, when we make a phone call, we don’t know its internal processing. In Apex, we can use abstract classes and Interfaces to achieve this.
Abstract classes can have constructors that are called when an instance of the inheriting class is created. However, the constructor of an abstract class cannot be called directly, but only through the constructor of the inheriting class.
- Abstract classes are like a template for other classes. Their methods and properties must be included in subclasses.
- A subclass must inherit from the abstract class to create an object. The subclass provides implementations for the abstract methods defined in the abstract class.
- We cannot create an object directly from an abstract class. We need to create objects from subclasses inherited by the abstract class.
- Abstract classes can also have non-abstract methods with an implementation. These methods can be called from inheriting classes without modification.
Syntax: Declaring Abstraction in Salesforce Apex
We need to add an abstract keyword before declaring the class and method. Because we only declare a method in the abstract class, we need to create another class to implement it.
public abstract class BaseClass{
public abstract void MethodName();
}
The new class that we created extends the parent class, and then, using the override keyword, we can access the abstract method from the parent class.
public class childClass extends BaseClass{
public override void MethodName() {
// implement logic;
}
}
Implement Abstraction in Salesforce Apex
In the steps below, I will explain how to implement abstraction using method overriding in Salesforce Apex.
We created an abstract class called AreaCalculator that defines the computeArea() method, but the actual implementation is left to its subclasses. The AreaCalculator class is abstract and only defines the method computeArea().
public abstract class AreaCalculator {
public abstract void computeArea();
}
Now, we have created another class named SquareArea, which extends the base class. In this class, we will implement the logic for the methods that we declared in the base class. The SquareArea can call computeArea().
public class SquareArea extends AreaCalculator {
public override void computeArea() {
integer side = 10;
decimal area;
area = 4 * side;
system.debug(' Area of Square is = ' +area );
}
}
Then, we created another class that extended the base class and overrode the abstract method from the base class.
public class RectangleArea extends AreaCalculator {
public override void computeArea() {
integer length = 10;
integer breath = 5;
decimal area;
area = length * breath;
system.debug(' Area of Rectangle is = ' +area );
}
}
As you execute the Apex code, you can see the method that we declared in the base class and implemented in the sub-classes performing different operations.
