Salesforce Apex is a powerful, object-oriented programming language tailored for the Salesforce platform. Understanding the principles of Object-Oriented Programming (OOP) in Apex is essential for building scalable, maintainable, and reusable code.
What is OOP in Apex?
OOP is a programming paradigm based on the concept of "objects", which can contain data and code. Apex supports all four core OOP principles:
Encapsulation:
Encapsulation allows you to bundle data (variables) and methods that operate on the data into a single unit—called a class. It also helps protect data by restricting access using access modifiers like private, public, and protected.
public class LibraryBook {
private String title;
private Boolean isCheckedOut;
public void checkOut() {
isCheckedOut = true;
}
public Boolean getStatus() {
return isCheckedOut;
}
}
Abstraction:
Abstraction hides complex implementation details and exposes only the necessary parts of an object. In Apex, this is achieved using abstract classes and interfaces.The Shape class defines an abstract method area(), and Circle provides its specific implementation.
public abstract class Payment {
public abstract void processPayment();
}
public class CreditCardPayment extends Payment {
public override void processPayment() {
System.debug('Processing credit card payment');
}
}
Inheritance:
Inheritance enables a class (child) to inherit properties and methods from another class (parent). This promotes code reuse and logical hierarchy.
public class Vehicle {
public void start() {
System.debug('Vehicle started');
}
}
public class Truck extends Vehicle {
public void loadCargo() {
System.debug('Cargo loaded');
}
}
Polymorphism:
Polymorphism allows methods to behave differently based on the object that is calling them. Apex supports both compile-time (method overloading) and runtime (method overriding) polymorphism.
Polymorphism lets you use a single interface to represent different types.
public virtual class Notification {
public virtual void send() {
System.debug('Sending generic notification');
}
}
public class EmailNotification extends Notification {
public override void send() {
System.debug('Sending email notification');
}
}
public class PushNotification extends Notification {
public override void send() {
System.debug('Sending push notification');
}
}
// Usage
Notification n1 = new EmailNotification();
Notification n2 = new PushNotification();
n1.send(); // Sending email notification
n2.send(); // Sending push notification
Explanation of compile-time polymorphism (method overloading) and runtime polymorphism (method overriding) in Apex, along with examples:
Compile-time polymorphism occurs when multiple methods in the same class have the same name but different parameter lists (type, number, or order). The method that gets called is determined at compile time.
Example:
public class Calculator {
public Integer add(Integer a, Integer b) {
return a + b;
}
public Decimal add(Decimal a, Decimal b) {
return a + b;
}
public Integer add(Integer a, Integer b, Integer c) {
return a + b + c;
}
}
Calculator calc = new Calculator();
System.debug(calc.add(2, 3)); // Calls method with two Integers
System.debug(calc.add(2.5, 3.5)); // Calls method with two Decimals
System.debug(calc.add(1, 2, 3)); // Calls method with three Integers
Runtime polymorphism occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method that gets executed is determined at runtime based on the object type.
Example:
public virtual class Animal {
public virtual void makeSound() {
System.debug('Animal makes sound');
}
}
public class Dog extends Animal {
public override void makeSound() {
System.debug('Dog barks');
}
}
public class Cat extends Animal {
public override void makeSound() {
System.debug('Cat meows');
}
}
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.makeSound(); // Outputs: Dog barks
a2.makeSound(); // Outputs: Cat meows
Why OOP Matters in Apex?
Using OOP in Apex helps developers:
- Write cleaner and more modular code
- Reduce redundancy
- Improve code maintainability
- Enhance collaboration in large projects