Object-Oriented Programming in Salesforce Apex — Apex Part 5

Mohammad Usman
5 min readMar 16, 2024

--

Salesforce Apex is a strongly typed, object-oriented programming language that allows developers to build robust and scalable applications on the Salesforce platform. As an integral part of the Salesforce ecosystem, Apex provides developers with tools to create custom business logic, automate processes, and integrate with external systems.

In this comprehensive guide, we will delve into the core concepts of object-oriented programming (OOP) in Apex. We’ll cover classes and objects, constructors and methods, access modifiers, and best practices for designing and implementing Apex code.

Salesforce development

Understanding Classes and Objects in Apex

Classes

In Apex, a class is a blueprint or template for creating objects. It defines the properties (variables) and behaviors (methods) that objects of that class will have. Classes in Apex are declared using the `class` keyword followed by the class name. Here’s an example of a simple Apex class:


public class Car {
// Properties
public String make;
public String model;
public Integer year;
// Constructor
public Car(String make, String model, Integer year) {
this.make = make;
this.model = model;
this.year = year;
}
// Method
public String getDetails() {
return 'Make: ' + make + ', Model: ' + model + ', Year: ' + year;
}
}

Objects

An object is an instance of a class. It represents a specific entity based on the blueprint provided by the class. Objects have their own set of properties and can invoke methods defined in the class. Here’s how you can create objects of the `Car` class:

Car myCar = new Car(‘Toyota’, ‘Camry’, 2022);
System.debug(myCar.getDetails()); // Output: Make: Toyota, Model: Camry, Year: 2022

Constructors and Methods

Constructors

A constructor is a special method that is invoked when an object of a class is created. It is used to initialize the object’s state. In Apex, constructors have the same name as the class and do not have a return type. There are two types of constructors: default constructor and parameterized constructor.

Default Constructor

If a class does not have any constructors defined, Apex provides a default constructor with no parameters. For example:

public class MyClass {
// Default constructor
public MyClass() {
// Initialization code here
}
}

Parameterized Constructor

A parameterized constructor accepts parameters to initialize the object’s state. It allows you to pass values during object creation. We’ve already seen an example of a parameterized constructor in the `Car` class above.

public class MyClass {
// Default constructor
public String make;
public String model;
public Integer year;
public MyClass(String make, String model, Integer year) {
// Initialization code here
this.make = make;
this.model = model;
this.year = year;
}
}

Methods

Methods are functions defined within a class that perform specific actions or provide functionality related to the class. They can access and manipulate the properties of the class. Methods in Apex can have access modifiers (public, private, global) to control their visibility and accessibility.

Public Methods

Public methods are accessible from outside the class. They can be invoked by other classes or triggers. Public methods are typically used to expose functionality to other parts of the application.

public class MyClass {
// Public method
public void myMethod() {
// Method implementation
}
}

Private Methods

Private methods are only accessible within the class in which they are defined. They cannot be invoked from outside the class. Private methods are useful for encapsulating logic that is internal to the class and not intended for external use.

public class MyClass {
// Private method
private void myPrivateMethod() {
// Method implementation
}
}

Global Methods

Global methods are accessible across different classes and namespaces within Salesforce. They are often used in managed packages or for integration purposes where external systems need to invoke Salesforce functionality.

global class MyGlobalClass {
// Global method
global void myGlobalMethod() {
// Method implementation
}
}

Access Modifiers: Public, Private, Global

Access modifiers control the visibility and accessibility of classes, variables, and methods in Apex. They ensure encapsulation and help in enforcing data hiding and abstraction principles.

Public

The `public` access modifier makes the class, variable, or method accessible from outside the class in which it is defined. It allows other classes to interact with and use the public entity.

public class MyClass {
public String myProperty;
public void myMethod() {
// Method implementation
}
}

Private

The `private` access modifier restricts the visibility of the class, variable, or method to only within the class in which it is defined. It cannot be accessed or invoked from outside the class.

public class MyClass {
private String myProperty;
private void myMethod() {
// Method implementation
}
}

Global

The `global` access modifier is specific to Salesforce and allows classes and methods to be accessed across different namespaces. It is often used for exposing functionality in managed packages or for integration purposes.


global class MyGlobalClass {
global String myProperty;
global void myMethod() {
// Method implementation
}
}

Best Practices for Object-Oriented Programming in Apex

1. Encapsulation: Encapsulate data by making variables private and providing public methods for accessing and modifying the data.
2. Modularity: Break down complex functionalities into smaller, modular components (classes and methods) for better maintainability and reusability.
3. Inheritance: Use inheritance to establish a hierarchy of classes and promote code reuse. However, be mindful of the limits of Salesforce inheritance due to the single-class inheritance model.
4. Polymorphism: Leverage interfaces and abstract classes to achieve polymorphic behavior, allowing different classes to be treated interchangeably.
5. Access Modifiers: Use access modifiers appropriately to control visibility and accessibility, promoting encapsulation and data hiding.
6. Testing: Implement unit tests to ensure the correctness and reliability of your code. Salesforce provides a robust testing framework for Apex code.
7. Governor Limits: Be mindful of Salesforce governor limits and design your code to optimize resource usage and avoid hitting limits.

Resources for Further Learning

To further enhance your understanding of advanced Apex features and Salesforce development in general, here are some recommended resources:

- Salesforce Apex Developer Guide: The official Apex developer guide provides comprehensive documentation and examples for mastering Apex programming.
- Trailhead: Salesforce’s interactive learning platform offers a wide range of modules and trails on Apex development, asynchronous processing, integrations, and more.
- Salesforce Developer Blog: Stay updated with the latest news, tips, and best practices from Salesforce developers and experts through the official developer blog.
- Stack Exchange — Salesforce: Engage with the Salesforce community, ask questions, and share knowledge on Stack Exchange’s dedicated Salesforce platform.

Conclusion

Object-oriented programming in Apex provides developers with powerful tools for building scalable and maintainable applications on the Salesforce platform. By understanding the core concepts of classes, objects, constructors, methods, and access modifiers, developers can design and implement robust solutions that meet the needs of their organizations.

In this guide, we’ve covered the fundamentals of object-oriented programming in Apex, including class and object creation, constructors and methods, and the use of access modifiers. We’ve also discussed best practices for designing and implementing Apex code.

By applying these principles and best practices, developers can write efficient, modular, and maintainable code that maximizes the capabilities of the Salesforce platform.

--

--

Mohammad Usman

Trailblazer | Transforming Businesses through Salesforce Expertise | Salesforce Technical Architect, Consultant & Developer | Technical Lead