- SalesforceChaCha
- Posts
- 💃 Classes, Interfaces, and OOP in Apex🕺
💃 Classes, Interfaces, and OOP in Apex🕺
The Building Blocks of Salesforce Wizardry
Good morning, Salesforce Nerds! Let’s imagine for a bit that you're the conductor of a symphony …
Where the violins, cellos, and percussion all play their part to create something harmonious. 😇
In the world of Apex, classes and interfaces are your instruments, and object-oriented programming (OOP) is your musical score.
Whether you're a novice or seasoned developer, understanding these concepts is the secret to orchestrating powerful, reusable, and scalable Salesforce solutions. 💥
Let’s check out how we can learn to play these instruments …

TABLE OF CONTENTS
Classes, Interfaces, and OOP in Apex
OO-WHAT?
WHAT IS OOP?
Object-oriented programming is a paradigm that organizes software design around objects, which are instances of classes. 🧩
These objects have properties (attributes) and behaviors (methods) that let them interact with each other to perform complex operations.
The four pillars of OOP are:
📦️ Encapsulation: Keep your variables and methods bundled together, limiting access to only what’s necessary.
🎛️ Abstraction: Hide complexity from the user by exposing only essential features.
🧒 Inheritance: Let classes share behaviors and attributes to reduce code duplication.
🔀 Polymorphism: Enable objects to take on many forms, making your code adaptable and flexible.
Apex embraces these OOP principles, allowing you to create dynamic and efficient solutions. 💯
APEX’S SWISS ARMY KNIFE
CLASSES
A class is a blueprint for creating objects. 🏗️
Think of it as a cookie cutter, where the cutter defines the shape, and the cookies are the objects.
Each class in Apex defines:
🫙 Attributes: Variables that store data about an object.
💨 Methods: Functions that define the object’s behavior.
Anatomy of an Apex Class
Here’s a simple example:
public class CookieCutter {
// Attributes
public String flavor;
public Integer quantity;
// Constructor
public CookieCutter(String flavor, Integer quantity) {
this.flavor = flavor;
this.quantity = quantity;
}
// Methods
public void bake() {
System.debug('Baking ' + quantity + ' ' + flavor + ' cookies.');
}
}
Breakdown:
🫙 Attributes: flavor
and quantity
store information about the cookies.
🚧 Constructor: Initializes the object when it’s created.
💨 Methods: Define what the object can do, like baking cookies.
Use Cases for Classes
Building utility classes for reusable methods.
Modeling real-world entities, like Accounts, Orders, or Spaceships.
Structuring logic for service layers or domain-specific operations.
THE CONTRACTUAL AGREEMENT
INTERFACES
An interface is like a contract; it defines methods a class must implement but doesn’t provide the logic for those methods. 📜
It’s like telling someone to bake cookies but not specifying the recipe.
Anatomy of an Interface
Here’s an example:
public interface IBakeable {
void bake();
}
public class CookieCutter implements IBakeable {
public void bake() {
System.debug('Baking cookies with a default recipe.');
}
}
Key Points:
🧁 The IBakeable
interface specifies the bake
method.
🍪 The CookieCutter
class implements the interface and provides the logic for bake
.
Use Cases for Interfaces
Standardizing method signatures across different classes.
Creating plug-and-play modules in large systems.
Enhancing testability by allowing for mock implementations.
BRINGING IT ALL TOGETHER
OOP IN ACTION
Let’s dive into a practical example to see classes, interfaces, and OOP principles at work:
The Scenario
You’re tasked with designing a bakery system. 🧑🍳
Different baked goods (cookies, cakes, pies) need a consistent way to "bake" but have unique baking processes.
Implementation
📜 Define an Interface
public interface IBakeable {
void bake();
}
📦️ Create Classes for Each Baked Good
public class Cookie implements IBakeable {
public void bake() {
System.debug('Baking delicious cookies!');
}
}
public class Cake implements IBakeable {
public void bake() {
System.debug('Baking a fluffy cake!');
}
}
public class Pie implements IBakeable {
public void bake() {
System.debug('Baking a scrumptious pie!');
}
}
🔀 Leverage Polymorphism
public class Bakery {
public static void bakeAll(List<IBakeable> items) {
for (IBakeable item : items) {
item.bake();
}
}
}
// Example Usage
List<IBakeable> items = new List<IBakeable>{new Cookie(), new Cake(), new Pie()};
Bakery.bakeAll(items);
Key Takeaways
📦️ Encapsulation: Each class contains its unique baking logic.
🎛️ Abstraction: The IBakeable
interface hides the specifics of how baking works.
🧒 Inheritance: While not directly used here, it could be applied to shared behaviors (e.g., all baked goods have a temperature attribute).
🔀 Polymorphism: The bakeAll
method treats all baked goods uniformly, thanks to the IBakeable
interface.
FINAL THOUGHTS
WHY IT MATTERS
Mastering classes, interfaces, and OOP in Apex isn’t just a rite of passage for Salesforce developers - it’s the key to writing clean, maintainable, and scalable code. 🗝️
Whether you’re building a small utility class or architecting a complex enterprise solution, these principles will be your guiding star.
So, the next time you dive into an Apex project, remember:
you’re not just writing code - you’re orchestrating a symphony, one class and interface at a time. 🎻
Happy coding!
SOUL FOOD
Today’s Principle
"Write shy code — modules that don't reveal anything unnecessary to other modules and that don't rely on other modules' implementations."
and now....Salesforce Memes



What did you think about today's newsletter? |