Apex is an object-oriented programming language developed by Salesforce for building custom logic and automation. It is similar to Java and integrates with Salesforce’s database (SOQL, DML) to handle server-side logic.
Here, I will explain the concept of object-oriented programming in Salesforce Apex, along with related concepts and examples in Apex.
- What is object-oriented programming in Salesforce Apex?
- Classes and Objects in Salesforce Apex.
- Encapsulation in Salesforce Apex.
- Inheritance in Salesforce Apex.
- Polymorphism in Salesforce Apex
- Abstraction in Salesforce Apex.
What is Object-Oriented Programming in Salesforce Apex?
The object-oriented programming concept in Salesforce Apex refers to the practice of organizing code into objects, which serve as blueprints for creating instances of data. It is a method of programming that uses ‘objects’ to create applications and computer programs.
OOPs concepts in Apex include classes, objects, inheritance, encapsulation, and polymorphism, which help in creating modular, reusable, and maintainable code.

Understanding OOPs Concept in Salesforce Apex
Below, I have explained all the concepts included in object-oriented programming in Salesforce Apex.
1. Classes and Objects in Salesforce Apex
An Apex Class in Salesforce is a blueprint of an object (instance). Classes define what information an object will hold and what it can do, such as its structure (properties) and methods, for Salesforce objects.
Syntax: Creating an Apex Class –
public class class_Name {
public void method_Name() {
//Enter your logcic;
}
}The Object is an instance of a class, like a container that holds information such as absolute values instead of variables.
Syntax: Creating an Apex Class Object –
class_Name obj = new class_Name();Here, I created a class with a public access modifier. Then, I declared a string and a parameterized constructor with the parameter as a string. Using this keyword, I assigned a name to the accountName and then displayed the variable using the debug method in the printAccountName() method.
public class Demo {
public String accountName;
public Demo (String name) {
this.accountName = name;
}
public void printAccountName() {
System.debug ( 'Entered Name : ' +accountName );
}
}
In the anonymous window, create an instance of a class using the new keyword, provide the string as accountName, and call the method. Execute the code.


Access Modifiers in Apex Class
Apex allows users to use private, public, protected, and global access modifiers when defining methods and variables. By default, a process or a variable is visible only to the apex code within the defining class.
If you wish for your method or variable to be public to other classes in the same application namespace, then you must specify it with the public keyword.
Types of access modifiers in the Apex class:
- Private: The private methods and variables are only accessible within the apex class in which it is defined.
- Protected: Using the protected modifier, the method or variable is visible to any inner class in the defining apex classes and the classes that extend the defining apex class.
- Public: The method or variable declared as public can be used by any apex in the application or namespace.
- Global: This access modifier is used for any method that needs to be referenced outside of the application, either in the API or by another apex code.
2. Encapsulation in Salesforce Apex
Encapsulation is object-oriented programming (OOP). It refers to the binding of data and methods that operate on the data into a single unit known as a class. In simpler terms, encapsulation hides an object’s internal state from the outside class and only exposes the necessary functionalities through methods.
Encapsulation ensures that class variables and methods are protected from unauthorized access. Use private, public, or protected access modifiers to control this.
Here, we created a balance variable with a decimal data type and declared it private. Now, in this class, encapsulation is applied by hiding the internal state of the object (the balance variable) and providing controlled access through public methods.
Then, we created a public method to find the balance amount during the deposit and withdrawal. These methods can only access the private variable(balance) if you access this variable outside the class, which doesn’t allow you.
The encapsulation hides the internal details of how the balance is stored and ensures and binds data and methods. It can only be modified or viewed through controlled methods, preventing unauthorized or invalid changes.
public class Demo {
private Decimal balance;
public Demo (Decimal initialBalance) {
if (initialBalance > 0) {
balance = initialBalance;
} else
{
balance = 0;
}
System.debug (' Initial Balance: ' + balance);
}
public Decimal getBalance() {
System.debug (' Current Balance: ' + balance);
return balance;
}
public void deposit (Decimal amount) {
if (amount > 0) {
balance = balance + amount;
System.debug(' Deposited: ' + amount + ', New Balance: ' + balance);
} else
{
System.debug (' Deposit amount must be positive.');
}
}
public void withdraw (Decimal amount) {
if (amount > 0 && amount <= balance) {
balance = balance - amount;
System.debug (' Withdrawn: ' + amount + ', New Balance: ' + balance);
} else {
System.debug ('Invalid withdrawal amount.');
}
}
}To execute the Apex code anonymous window, pass the balance value to the methods and click the Execute button.

In the Execution log, you will see the output as per the logic you entered in the method for depositing or withdrawing the amount.

3. Inheritance in Salesforce Apex
Inheritance is a fundamental concept in object-oriented programming (OOP) that enables a class (referred to as a child or subclass) to inherit properties and methods from another class, using the ‘extend‘ keyword (also known as a parent or superclass). To implement inheritance in Apex, we need to use the virtual or abstract keyword in the base class and methods.
In Salesforce Apex, inheritance enables developers to create efficient, reusable code by defining standard functionality in a parent class and extending or customizing it in child classes.
Syntax: Creating Inheritance in Apex –
Create an Apex class and give it a name according to your requirements. When you want to extend the class or create an inheritance, make sure you add the virtual keyword before the class.
public virtual class className {
public void methodName() {
//logic;
}
}Then, create another class extending the base class you created. You need to add the extends keyword before entering the baseclass name.
public class newClass extends className {
public void methodName1() {
//logic;
}
} For example, you are creating an inheritance for a car as a base class, and by inheriting the baseclass, you are creating child classes for the car model.
Here, we created a class with a virtual keyword and a method that has information about car details.
public virtual class Car {
public void carBrand() {
String brand = 'TATA';
Date Manufactured = Date.newInstance(2020,01,18);
Decimal Price = 735000;
system.debug('Car Brand : ' +brand);
system.debug('Car Date Manufactured : ' +Manufactured);
system.debug('Car Price : ' +Price);
}
}
Then, we created another class, which was inherited from the car class using the extends keyword. Here, we created a method in which the variable stores the car name, and then, using the super keyword, we called the carBrand() method, which is from the base class.
public class CarModel extends Car {
public void carName() {
String Name = 'Altroz';
system.debug('Car Name : ' +Name);
Super.carBrand();
}
}
Open an anonymous window to execute the Apex code. Here, we created an instance of the child class and called the child class method. Since we called the base class method in the child class using the super keyword, the child class inherits all the information from the base class method.

As you execute the child class method, you can also see the information of the base class method.

4. Polymorphism in Salesforce Apex
Polymorphism is nothing but many forms. In Apex, if you want to modify the parent class method in the child class, you need to use the override keyword in the child class. The override keyword informs Apex that a new version of the same method is available in the child class. We can also access the parent class method with the help of the Super keyword.
Overriding means having two or more methods with the same name and parameters in different classes. It is also called dynamic or run-time polymorphism.
For example, we will create an inheritance and create a similar method in every class, which means we will override the method from the base class to the child class.
Polymorphism can be done in inheritance because we need to create the same method in different classes. In the base class, we declared method.
public virtual class Shape {
public virtual void CalculateArea() {
system.debug(' **** This is polymorphism in Salesforce Apex ****');
}
}Now, create a new class that extends the base class, and while adding the method that we created in the base class, we need to use the override keyword.
public class Triangle extends shape {
public override void CalculateArea() {
decimal base = 10;
decimal Height = 5;
decimal area;
area = 0.5 * base * Height;
system.debug(' Area of Triangle is = ' +area );
}
}It is the same for the other class; also, create a class and add the same method.
public class Rectangle extends shape {
public override void CalculateArea() {
decimal Length = 10;
decimal breath = 5;
decimal area;
area = Length * breath;
system.debug(' Area of Rectangle is = ' +area );
}
}Now, execute the class by creating an instance variable and calling the method that you want using the particular class instance.

In the output, you can see we created the same methods in different classes, but they perform different operations for that particular class.

5. Abstraction in Salesforce Apex
In the Salesforce Apex programming language, abstraction is used to hide certain details and show only the essential features of the 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.
We created an abstract class called AreaCalculator that defines the method computeArea(), but the actual implementation is left to the 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.

These are the pillars of object-oriented programming concepts in Salesforce Apex.
Conclusion
I hope you now understand the concept of object-oriented programming in Salesforce Apex. In this article, we have seen OOPs, Apex classes and objects with syntax and examples, encapsulation in Apex, the Inheritance concept, polymorphism, and Abstraction in Salesforce Apex.
You may also like to read:
- Interfaces in Salesforce Apex and How to Use it
- Logical and Looping Statements in Salesforce Apex Programming

Shubham is a Certified Salesforce Developer with technical skills for Building applications using custom objects, approval processes, validation rule salesforce flows, and UI customization. He is proficient in writing Apex classes, triggers, controllers, Apex Batches, and bulk load APIs. I am also familiar with Visualforce Pages and Lighting Web Components. Read more | LinkedIn Profile