Abstraction in Salesforce Apex

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.

What is Abstraction in Salesforce Apex

Implement Apex Abstraction in Salesforce Object

In the Apex code below, I explain how to implement Apex abstraction in the Salesforce object, allowing us to hide unnecessary details and display only essential data.

In the example below, I created an abstract class and an abstract method. However, I didn’t implement the method in this class because declaring any abstract method should be implemented in an inherited class. We used this method to create records for the SObjects.

First, we created an abstract class and an abstract method named CreateRecord(). However, we didn’t implement the abstract method in this class; we only declared it.

public abstract class ApexAbstraction {
    public abstract void CreateRecord(); 
}

We created another class that extends the base class to implement the abstract method. Here, we can implement the method that we declared in the abstract class.

To implement the logic in the method, we need to add an override keyword to access that method in this class.

In this method, we created an account record using the List collection and inserted it into the Salesforce database using the DML statement.

public class CreateAccount extends ApexAbstraction {
    public override void CreateRecord() {
        List<Account> Alist = new List<Account>();
        Account acc = new Account();
        acc.Name = 'Account by Abstraction Method';
        acc.Rating = 'Hot';
        acc.Phone = '7757957411';
    Alist.add (acc);
    Insert Alist;
    System.debug('Account with Name : '+acc.Name +'has been successfully created...');
    }
}

Again, we created a different class to implement another logic for the abstract method. Here, we also accessed the method using the override keyword and implemented logic for the created lead record.

public class CreateLead extends ApexAbstraction {
    public override void CreateRecord() {
        List<Lead> LeadList = new List<Lead>();
        Lead l = new Lead();
        l.FirstName = 'Lead by Abstraction Method';
        l.LastName = 'Leads';       
        l.Company = 'TSinfo Technologies';       
        l.LeadSource = 'Web';
        l.Status = 'Open-Contacted';
        LeadList.add(l);
    Insert LeadList;
    System.debug('Lead with Name : '+l.Name +'has been successfully created...');
    }
}

Now, to execute the abstraction class, we cannot create an abstract class object; instead, we need to create an object of a class that extends the abstract class and its methods.

Then, by creating an instance of a class that implements the abstract method, we implemented it in the child class.

Abstraction in Salesforce Apex

As you execute the code, navigate to the object tab for which you created records. There, you will see that the records have been created successfully.

Implement Abstraction in Salesforce Apex

In this way, we can use abstraction in Salesforce Apex to manage and understand by hiding unnecessary details and only showing essential details.

Conclusion

I hope you have an idea about abstraction is Salesforce Apex. In this tutorial, I have explained what it is and how to use it. Using an example, I explained how to implement abstraction in Salesforce Apex to hide unnecessary details and display only essential features to the user.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.