Hot Posts

Introduction to Oops Concepts

Introduction of Object Oriented Programming
OOPs Concepts in Apex - Classes, Objects, Inheritance, Polymorphism

This tutorial is targeted for Salesforce programmers beginning to learn Apex. This will bring you to an intermediate level of expertise in Apex programming, covering all the important aspects of Apex with complete hands-on code experience.

Object Oriented Programming in Salesforce

Object means a real-world/run-time entity such as a marker, car, table, chair, etc. Object-Oriented Programming is a methodology or way to design a program using classes and objects. It eases the software development and its maintenance by providing some beautiful concepts as followed:

  • Classes
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Abstraction

Object

Any real-world entity that has state and behavior is known as an object. For example: marker, book, table, chair, mouse, car, etc. Technically, we can say an object is an instance of a class, or in other words, you can say it is an implementation of a class.

Class

A class is a concept, prototype, or template; it is a logical entity. Technically, we can say that we create an individual object of a class.

 Example:-
 // Class definition
public class Car {
   // Properties
   public String color;
   public String model;
   
   // Method
   public void displayDetails() {
       System.debug('Car Model: ' + model + ', Color: ' + color);
   }
}

// Creating an object of the class
Car myCar = new Car();
myCar.color = 'Red';
myCar.model = 'Tesla Model S';
myCar.displayDetails(); // Output: Car Model: Tesla Model S, Color: Red
 

Encapsulation

It is a binding of code and data together into a single unit known as encapsulation. For example, a capsule is wrapped with different types of medicines into a single unit. An Apex class is an example of encapsulation.

 Example:-
  public class BankAccount {
   // Private variables
   private Decimal balance;
   
   // Public method to set balance
   public void setBalance(Decimal bal) {
       if(bal >= 0) {
           balance = bal;
       }
   }
   
   // Public method to get balance
   public Decimal getBalance() {
       return balance;
   }
}

// Using encapsulation
BankAccount account = new BankAccount();
account.setBalance(1000);
System.debug('Balance: ' + account.getBalance()); // Output: Balance: 1000

Polymorphism

One name, many forms, known as polymorphism. A real-world example of polymorphism: A person at the same time can have different characteristics. Like a woman at the same time is a mother, a wife, an employee, and a daughter, etc. In Apex, we use method overloading and method overriding to achieve polymorphism.

Polymorphism allows methods to do different things based on the object it is acting upon. It is achieved through method overloading and method overriding.
Example of Method Overloading:
public class MathOperations {
   // Method to add two integers
   public Integer add(Integer a, Integer b) {
       return a + b;
   }
   
   // Method to add three integers
   public Integer add(Integer a, Integer b, Integer c) {
       return a + b + c;
   }
}

// Using method overloading
MathOperations math = new MathOperations();
System.debug(math.add(5, 10)); // Output: 15
System.debug(math.add(5, 10, 15)); // Output: 30
Example of Method Overriding:
// Parent class
public class Shape {
   public void draw() {
       System.debug('Drawing a shape');
   }
}

// Child class overriding the draw method
public class Circle extends Shape {
   public void draw() {
       System.debug('Drawing a circle');
   }
}

// Using method overriding
Shape myShape = new Circle();
myShape.draw(); // Output: Drawing a circle
 

Inheritance

When one class acquires all the properties and behaviors of a super/parent class, it is known as inheritance. It provides code reusability as well as the ability to achieve runtime polymorphism. Note: Without inheritance, we cannot achieve runtime polymorphism.

Inheritance allows a class (child class) to inherit properties and methods from another class (parent class).
Example:

// Parent class
public class Animal {
   public void eat() {
       System.debug('This animal eats food.');
   }
}
// Child class inheriting from Animal
public class Dog extends Animal {
   public void bark() {
       System.debug('The dog barks.');
   }
}
// Using inheritance
Dog myDog = new Dog();
myDog.eat(); // Output: This animal eats food.
myDog.bark(); // Output: The dog barks.

Abstraction

Hiding internal complexity and showing functionality is known as abstraction. For example: Car Drive, we don’t know the internal processing. In Apex, we use abstract classes and interfaces to achieve abstraction.

Abstraction is the concept of hiding the complex implementation details and showing only the essential features of the object.


Example:

public abstract class Vehicle {
   // Abstract method
   public abstract void start();
   
   // Concrete method
   public void stop() {
       System.debug('Vehicle stopped.');
   }
}

// Concrete class inheriting from abstract class
public class Bike extends Vehicle {
   public void start() {
       System.debug('Bike started.');
   }
}

// Using abstraction
Bike myBike = new Bike();
myBike.start(); // Output: Bike started.
myBike.stop(); // Output: Vehicle stopped.
 

Oops Video

What is Apex in Salesforce?

Apex is a programming language developed by Salesforce. It is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce platform.

How Apex Works in Salesforce

Apex works by allowing developers to write and execute custom logic on the Salesforce platform. This includes creating custom business logic, automating processes, and integrating with external systems.

Note: It’s a case insensitive language.

  • Apex syntax looks mostly like a Java programming language.
  • Apex allows developers to write business logic to the record save process.
  • Apex has built-in support for unit test creation and its execution.

Apex Provides Built-in Support for the Following:

  • Data Manipulation Language (DML) calls to insert, update, and delete records.
  • Inline SOSL or SOQL statements for retrieving records from sObjects.
  • Looping control structures that help with bulk processing.
  • A record locking syntax that prevents record conflicts.
  • Custom public API calls.
  • Sending and receiving emails.
  • Web services (REST/SOAP) request/response integrations.
  • Warnings and errors to prevent sObjects referenced by Apex from being modified.

As a language, Apex is integrated, very easy to use, data-focused, rigorous, hosted, multi-tenant aware, automatically upgradable in nature, easy to test, and versioned.

When developers write and save their code on the platform, it is compiled on the force.com platform and stored in the form of metadata in Salesforce servers. End users can send requests from the UI and retrieve results from Salesforce servers.

Capabilities of Apex

When to Use Apex Programming?

Apex should be used as a solution when:

  • You need to write complex business logic to rows of data being saved by any means.
  • You need to create additional web services API functionality for exposing logic either within Salesforce or outside of Salesforce.
  • You need to call out to an external web service and process the results.
  • You need to manage incoming or outgoing emails in ways more complex than the declarative functionality.
  • Apex triggers execute no matter how the triggering data is being saved.
  • Apex executes regardless of whether the action originates in the user interface, through AJAX toolkit, or from web services API.
  • If you only want the code to execute through the UI, consider making a Visualforce page and controller.

Difference Between Traditional Programming and Apex Programming

Traditional code is fully flexible and can tell the system to do anything as per the requirement.
Apex is a governed language and can only do what the system allows.

Post a Comment

0 Comments