Salesforce is a powerful platform for managing customer relationships, and at its core, it provides Apex—a strongly-typed, object-oriented programming language that allows developers to customize and automate processes. If you’re new to Apex, understanding its fundamentals starts with grasping object-oriented programming (OOP). Let’s break it down and dive into writing Apex classes in Salesforce!
Understanding Object-Oriented Programming (OOP)
OOP is a programming paradigm that structures software around objects rather than functions. Objects are instances of classes, which act as blueprints defining attributes (variables) and behaviors (methods). OOP enhances code modularity, reusability, and maintainability.
The four main principles of OOP are:
- Encapsulation – Bundling data and methods within a class and restricting direct access to some of an object’s details.
- Abstraction – Hiding complex implementations and exposing only the necessary details.
- Inheritance – Allowing a class (child) to derive properties and behaviors from another class (parent).
- Polymorphism – Enabling objects to be treated as instances of their parent class, even when they have distinct implementations.
Writing Your First Apex Class
In Salesforce, Apex classes are used to define logic that can be executed on the platform. Here’s how you can create a basic Apex class:
public class HelloWorld {
// Property
public String greeting;
// Constructor
public HelloWorld(String name) {
this.greeting = 'Hello, ' + name + '!';
}
// Method
public String sayHello() {
return greeting;
}
}
Explanation:
public class HelloWorlddefines a class namedHelloWorld.public String greeting;is a property that stores a message.public HelloWorld(String name)is a constructor that initializes the greeting message when an instance is created.public String sayHello()is a method that returns the greeting.
Instantiating and Using Apex Classes
You can create an instance of this class and call its method in Salesforce’s Developer Console:
HelloWorld hw = new HelloWorld('Salesforce Developer');
System.debug(hw.sayHello());
This will output:
Hello, Salesforce Developer!
Implementing Object-Oriented Principles in Apex
Encapsulation: Using Private Variables
Encapsulation restricts direct access to an object’s data by using private variables and exposing them through public getter and setter methods.
public class Person {
private String name;
public void setName(String newName) {
this.name = newName;
}
public String getName() {
return this.name;
}
}
Inheritance: Extending a Class
Inheritance allows a child class to inherit properties and methods from a parent class.
public class Animal {
public void makeSound() {
System.debug('Some generic sound');
}
}
public class Dog extends Animal {
public void makeSound() {
System.debug('Bark!');
}
}
When calling:
Dog myDog = new Dog();
myDog.makeSound();
Output:
Bark!
Polymorphism: Overriding Methods
Polymorphism allows child classes to override methods from a parent class while maintaining a consistent interface.
public virtual class Vehicle {
public virtual void move() {
System.debug('Vehicle is moving');
}
}
public class Car extends Vehicle {
public override void move() {
System.debug('Car is driving');
}
}
When executing:
Vehicle myVehicle = new Car();
myVehicle.move();
Output:
Car is driving
Conclusion
Mastering Apex classes in Salesforce begins with understanding object-oriented principles. By using encapsulation, inheritance, and polymorphism, you can create robust, reusable, and scalable code. Whether you’re building automation, integrations, or custom applications, Apex classes are fundamental to Salesforce development.
Now, go ahead and experiment with Apex in the Developer Console, and start building smarter Salesforce solutionAs!
Additional Helpful Resources
A great resource to learn about more advanced APEX development patterns is this book by Dan Appleman called Advanced Apex Programming. I highly recommend it!

We would love to hear your comments!