Class and Object in Apex

Apex Classes and Objects in Salesforce Apex

In Salesforce Apex, classes and objects are foundational concepts that allow developers to model real-world entities and their behaviors in code. Apex is an object-oriented programming language, which means that the core building blocks of any Apex program are objects, and these objects are defined by classes.

Understanding Apex Classes

Classes in Apex serve as blueprints or templates for creating objects. They encapsulate data for the object (through variables) and the behavior of the object (through methods). A class can have attributes (variables) that represent the state of an object and methods that define what the object can do.

Key Components of an Apex Class:
  • Attributes (Variables): These are the properties of the class. They define the characteristics or data associated with objects of the class.
  • Methods: These define the actions or behaviors that objects of the class can perform. Methods can modify the object’s state or perform calculations.
  • Constructors: Special methods used to initialize objects when they are created.
  • Access Modifiers: Define the visibility of the class and its methods/variables (e.g., public, private, global).
Example: Creating an Apex Class

Let’s consider a scenario where we need to model a Book in a library system.

public class Book {
    // Attributes (Variables)
    public String title;
    public String author;
    public Integer pages;
    public Boolean isCheckedOut;

    // Constructor
    public Book(String title, String author, Integer pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
        this.isCheckedOut = false;  // By default, the book is not checked out
    }

    // Method to check out the book
    public void checkOut() {
        if (!this.isCheckedOut) {
            this.isCheckedOut = true;
            System.debug(this.title + ' has been checked out.');
        } else {
            System.debug(this.title + ' is already checked out.');
        }
    }

    // Method to return the book
    public void returnBook() {
        if (this.isCheckedOut) {
            this.isCheckedOut = false;
            System.debug(this.title + ' has been returned.');
        } else {
            System.debug(this.title + ' was not checked out.');
        }
    }
}

Explanation:

  • Attributes: The class Book has four attributes: title, author, pages, and isCheckedOut. These attributes hold the state of a Book object.
  • Constructor: The constructor Book(String title, String author, Integer pages) is used to initialize the attributes when a new Book object is created. For example, when you create a new Book, you provide a title, author, and number of pages.
  • Methods: The checkOut() method changes the state of the isCheckedOut attribute to true and prints a message. The returnBook() method changes the state of isCheckedOut back to false and prints a message.

Understanding Apex Objects

An object in Apex is an instance of a class. While a class defines the structure and behavior, an object represents a specific entity with the defined structure and behavior.

Example: Creating and Using Apex Objects

Using the Book class, we can create multiple book objects:

// Creating objects (instances of the class Book)
Book book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 180);
Book book2 = new Book('1984', 'George Orwell', 328);

// Using methods on the objects
book1.checkOut();    // Output: The Great Gatsby has been checked out.
book2.checkOut();    // Output: 1984 has been checked out.

book1.returnBook();  // Output: The Great Gatsby has been returned.
book2.returnBook();  // Output: 1984 has been returned.

Explanation:

  • Creating Objects: Book book1 = new Book('The Great Gatsby', 'F. Scott Fitzgerald', 180); creates an instance of the Book class. Here, book1 is an object of the Book class.
  • Using Methods: You can call methods on objects. For example, book1.checkOut(); changes the isCheckedOut attribute of the book1 object and prints a message.

Tutorials Deck

TutorialsDeck is striving to provide the best learning material on technical and non-technical subjects.

Languages

Web Technologies

Database

Trending Technologies

© 2024. All rights reserved.

Contact Us @ tutorialsdeck06@gmail.com