Object-Oriented
Learning Content
Last updated: July 20, 2026
Object-Oriented Programming (OOP)
Apex is an Object-Oriented Programming (OOP) language. This means developers can organize their code into reusable classes and objects, making applications easier to understand, maintain, and extend.
Instead of writing the same logic repeatedly, you can create reusable components such as Classes, Objects, Methods, Constructors, and Properties. These components help developers build clean, scalable, and maintainable applications.
💡 LearnFrenzy Insight
Think of a Class as the blueprint of a house and an Object as the actual house built using that blueprint. You can build thousands of houses from the same blueprint, just as you can create many objects from a single class.
Why Do We Need Object-Oriented Programming?
Imagine an application that manages Accounts, Contacts, Opportunities, Products, Orders, and Invoices. If every piece of logic were written in one large file, the application would become difficult to maintain.
Object-Oriented Programming solves this problem by organizing related functionality into separate classes. Each class has a specific responsibility, making the application modular and easier to test.
| Without OOP | With OOP |
|---|---|
| Business logic scattered across multiple files. | Business logic organized into reusable classes. |
| Duplicate code. | Reusable methods reduce duplicate code. |
| Difficult to maintain. | Easy to modify and extend. |
| Testing becomes complicated. | Classes can be tested independently. |
Core OOP Concepts in Apex
| Concept | Description |
|---|---|
| Class | A blueprint used to define properties and methods. |
| Object | An instance of a class. |
| Method | A block of reusable code that performs a specific task. |
| Constructor | A special method that initializes an object. |
| Encapsulation | Bundles data and methods into a single unit. |
| Inheritance | Allows one class to inherit features from another class. |
| Polymorphism | Allows methods to behave differently based on implementation. |
| Abstraction | Hides implementation details while exposing only required functionality. |
Example 1: Creating Your First Apex Class
The following example creates a simple Apex class that returns a welcome message.
public class WelcomeService {
public static String getMessage(){
return 'Welcome to LearnFrenzy Apex Tutorial';
}
}
Output
Welcome to LearnFrenzy Apex Tutorial
How This Code Works
| Code | Explanation |
|---|---|
public class WelcomeService |
Creates an Apex class named WelcomeService. |
public static |
The method can be called without creating an object. |
String |
The method returns text. |
return |
Returns the welcome message. |
Example 2: Creating an Object
Let's create an object from a class and call one of its methods.
public class Employee {
public String employeeName;
public void display(){
System.debug('Employee : ' + employeeName);
}
}
Create Object
Employee emp = new Employee();
emp.employeeName = 'Rahul';
emp.display();
Output
Employee : Rahul
🏢 Real Business Scenario
Suppose your company wants to calculate yearly bonuses for employees.
Instead of writing the bonus calculation multiple times, create an EmployeeService class with a reusable calculateBonus() method. Any trigger, Flow, or Lightning Web Component can call the same method, reducing duplicate code and making maintenance easier.
Example 3: Reusable Business Logic
public class DiscountService{
public static Decimal calculateDiscount(
Decimal amount,
Decimal percentage
){
return amount * percentage / 100;
}
}
Calling the Method
Decimal discount =
DiscountService.calculateDiscount(10000,10);
System.debug(discount);
Output
1000
This approach allows the same method to be reused across multiple classes without rewriting the discount calculation logic.
✅ Best Practice
Keep each class focused on a single responsibility. For example, create separate classes for Account operations, Opportunity calculations, Email notifications, and Integration logic instead of combining everything into one large class.
⚠️ Common Mistake
Avoid creating "God Classes" that contain hundreds of methods and handle multiple business processes. Such classes become difficult to understand, test, and maintain.
🎯 Interview Tip
A common interview question is: "Why is Apex an Object-Oriented Programming language?"
A strong answer is: Apex supports classes, objects, encapsulation, inheritance, polymorphism, and abstraction, allowing developers to create reusable, modular, and maintainable applications.
Quick Revision
- Object-Oriented Programming helps organize code into reusable classes and objects.
- A Class is a blueprint, while an Object is an instance of that class.
- Methods allow you to write reusable business logic.
- OOP improves maintainability, readability, and scalability.
- Following the Single Responsibility Principle keeps Apex applications clean and easier to test.
🚀 Next Part: Feature 3 – Strongly Typed Language
You'll learn how Apex enforces data types, why type safety matters, implicit vs explicit casting, and common type conversion examples with expected outputs.
Practice What You've Learned
Test your understanding with these practice exercises