Unleashing the Power of Classes, Objects, and Methods in Apex

Unleashing the Power of Classes, Objects and Methods in Apex

Table of content

  • What is Class?
  • Characteristics of classes in Apex
  • What is  Object?
  • What is Methos?

What is Class?

A class is a blueprint or template for constructing objects in Apex, Salesforce’s proprietary programming language. Apex is an object-oriented programming (OOP) language, and classes are important in organizing and structuring code on the Salesforce platform.

Characteristics of classes in Apex

  • Object-Oriented Nature: Classes are essential to the object-oriented architecture of Apex, which is an object-oriented language. They encompass the behavior (methods) and data (attributes) associated with a specific notion.
  • Attributes (Properties): Attributes are variables that hold information about a class. They specify the state of an object made using that particular class.
public class Car {
        public String color;
        public String model;
        public Integer year;
    }
  • Methods (Functions): Methods are functions linked to a class that specify how objects derived from that class are to behave.
 public class Car {
    public void startEngine() {
        System.debug('Engine started!');
    }

    public void accelerate() {
        System.debug('Car is accelerating.');
    }

    public void brake() {
        System.debug('Brakes applied.');
    }
}
  • Access Modifiers: Apex controls the visibility and accessibility of class members (attributes and methods) using access modifiers like public and private.
  • Constructors : Class-specific functions called constructors are used to initialize an object’s state upon formation. In Apex, the constructor and the class share the same name.
public class Car {
        public String color;
        public String model;

        // Constructor
        public Car(String initialColor, String initialModel) {
            color = initialColor;
            model = initialModel;
        }
    }
  • Inheritance: Apex enables inheritance, which allows a new class (subclass) to inherit attributes and methods from a parent class (superclass). For inheritance, the extends keyword is utilized.
public class Sedan extends Car {
        public Boolean hasTrunk;
    }
  • Encapsulation : Apex facilitates encapsulation by giving you the ability to manage class member visibility. It is possible to make attributes private or protected, which restricts direct access and promotes the usage of getter and setter functions.
  • Static and Instance Members: Both static and instance members are permitted in apex classes. While instance members are exclusive to each instance of the class, static members are part of the class itself.
 public class MathOperations {
        public static Integer add(Integer a, Integer b) {
            return a + b;
        }
    }

What is Object?

An instance of a class is referred to as an object in Apex. A class is an outline or model that specifies an object’s composition and actions. Similarly, using the Apex programming language, objects represent instances of the class and real-world entities.

  • Instance of a Class: An object is an instance of a class that is formed based on it. The object represents a particular instance or entity based on the template provided by the class.
//Example

    // Class definition
    public class Car {
        public String color;
        public String model;
    }
    
    // Creating an object (instance) of the class
    Car myCar = new Car();
  • Constructor :The constructor of the class is invoked upon object creation in order to initialize the object’s state. Special methods bearing the class name are called constructors.
 // Class definition with a constructor
    public class Car {
    public String color;
    public String model;

    // Constructor
    public Car(String initialColor, String initialModel) {
        color = initialColor;
        model = initialModel;
    }
    }

    // Creating an object with constructor parameters
    Car myCar = new Car('Red', 'Convertible');
  • Dynamically Typed: Since Apex is a dynamically typed language, an object’s type is decided upon at runtime. Although it gives for flexibility, treating different data kinds carefully is necessary.
  • Object Reference: References are used to manipulate objects. The reference to the object is really handled when it is provided as a parameter or allocated to a variable.
//Example : // Object reference

    Car myCar = new Car();
    Car anotherCar = myCar; // Both variables reference the same object

What is Methos?

A method in Apex is a piece of code that executes one or more specific operations inside of a class. A class’s methods are functions that specify how objects derived from that class are to behave. Apex methods are capable of returning a value and having parameters.

Here are some key aspects of methods in Apex Salesforce

Method Syntax

[modifiers] return_type method_name([parameters]) {
    // Method body
}
  • Modifiers : Modifiers for optional access such as public, private, protected, or global to regulate the method’s accessibility.
  • Access Modifiers

public: The method is accessible from outside the class.
private: The method is only accessible within the class.
protected: The method is accessible within the class and its subclasses.
global: The method is accessible from other classes in different namespaces (used for managed packages).

  • Return Type : The type of data that a method will return can be specified by its return type. Use void when a method returns null or nothing.
  • Parameters : In order to receive input values, methods can accept parameters. It is optional to use parameters.

Here’s a simple example:

public class MyClass {
    public Integer addNumbers(Integer a, Integer b) {
        return a + b;
    }
}

In this example:

=> public is the access modifier.
=> Integer is the return type.
=> addNumbers is the method name.
=> (Integer a, Integer b) are the parameters.

You can then create an instance of the class and call the method:

MyClass obj = new MyClass();
Integer result = obj.addNumbers(5, 7);
System.debug(result); // Outputs 12

Watch our entire video

Unleashing the Power of Classes, Objects and Methods in Apex

SUBSCRIBE OUR YOUTUBE CHANNEL

We provide all types of salesforce videos on this channel. Admin, Developer, Architect, and New Release Facts, Subscribe to our channel to stay up to date on everything Salesforce.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *