Apex Classes & Objects

Apex Classes & Objects

In Apex (like other object-oriented languages), a class is a blueprint for creating objects. It defines the properties (called variables or fields) and behaviors (called methods or functions) of real-world entities. 

An object is a specific instance of a class that holds real values for the properties defined in the class. 

1. Creating a Class

A class in Apex starts with the class keyword followed by the class name. 

Syntax: 

public class ClassName {

// Properties (Variables)

// Methods (Functions)

}

2. Creating an Object from a Class

An object is a real-world version of a class. It’s created using the new keyword. 

Syntax: 

ClassName objectName = new ClassName();

Examples

Student s1 = new Student();

s1.name = ‘Adnan’;

s1.age = 22;

s1.displayDetails();

Code Explaination

 • Student is the class (blueprint).
• s1 is the object (a real student created from the blueprint).
• We assign values using the dot (.) operator: s1.name = ‘Adnan’.
• Then we call a method: s1.displayDetails() to print the data.

Real-Life Analogy: "Class vs Object"

• A class is like the design of a mobile phone model. An object is the real phone in your hand built from that design.

You can create many phones (objects) from one design (class). 

Code Example

1: Simple Class with Properties and Method

public class Student {
public String name;
public Integer age;

public void displayInfo() {
System.debug(‘Name: ‘ + name);
System.debug(‘Age: ‘ + age);
}
}

Student s1 = new Student();
s1.name = ‘Adnan’;
s1.age = 22;

Code Explaination

public class Student – defines a class named Student.
name and age – variables that store info about a student.
displayInfo() – method that prints the student’s info.
Student s1 = new Student() – creates an object of the Student class.
s1.name = ‘Adnan’ – assigns value to the object’s variable.
s1.displayInfo() – calls the method to print the data.

Output:

Name: Adnan Age: 22

2: Class with Constructor

A constructor is a special method that runs automatically when the object is created. 

public class Car {
public String brand;
public Integer year;

// Constructor
public Car(String b, Integer y) {
brand = b;
year = y;
}

public void showDetails() {
System.debug(‘Brand: ‘ + brand);
System.debug(‘Year: ‘ + year);
}
}

Car c1 = new Car(‘Toyota’, 2022);
c1.showDetails();

Code Explaination

• Car(String b, Integer y) – constructor that initializes brand and year.
• c1 is created and initialized in one step: new Car(‘Toyota’, 2022).
• .showDetails() displays the values.

Output:

Brand: Toyota
Year: 2022

3: Method that Returns a Value

public class Calculator {
public Integer add(Integer a, Integer b) {
return a + b;
}
}

Calculator calc = new Calculator();
Integer result = calc.add(10, 20);
System.debug(‘Sum is: ‘ + result);

Code Explanation:

Method add() takes two numbers and returns their sum.
calc.add(10, 20) returns 30.
Output is printed using System.debug.

Output:

Sum is :30

Tasks for Practice

Task 1: Create a class Book with title, author, and a method printDetails(). Create an object and print the book details.

Task 2: Create a class Employee with a constructor that accepts name and salary. Print the details.

Task 3: Create a class Rectangle with length and width. Add a method calculateArea() that returns the area.

Task 4: Create a class Calculator with methods: add, subtract, multiply, divide. Call each method and print the result.

Task 5: Create a class Movie with title, genre, and rating. Create an object and write a method isRecommended() that returns true if the rating is more than 8.

Task 6: Create a class called Book with the following properties:

String title
String author
Double price Then, create an object of the class and print its values.

Task 7: Create two classes:

Greeting class with a method sayHello() that prints “Hello, Salesforce Learner!”
MainClass with the mainMethod() where you create a Greeting object and call its method.