Object-Oriented Programming In Salesforce

Introduction

We have the Object-Oriented Programming concepts used in multiple languages like Java, C++, Apex, etc. It focuses on the classes and objects and helps developers to create large and complex programs which are maintained very easily.

What is Object Oriented Programming?

Object Oriented Programming (OOP) is a programming model which is based on classes and objects. It’s a concept through which we can solve our problems while relating them to real concepts. We write our code inside the boundary, which is a class. We create an instance of a class to call functionalities inside the class. Apex variables and methods are private by default.

Object-Oriented Programming Concept in Salesforce

The following are the four main pillars of Object-Oriented Programming (OOPs) in Salesforce:.

1. Inheritance: It creates a parent-child relationship between Apex classes means the child class can inherit the properties and methods from the parent class using the ‘extend’ keyword, which enables the reusability of code.

The only class with virtual or abstract keywords can be inherited in Apex.

2. Abstraction: This concept is basically used to hide unnecessary or internal details and expose only necessary information, increasing security to a greater extent.

Abstraction can be achieved by using the Abstract class (where the class should have at least one abstract method and whose instance can never be created) and Interfaces (where all the methods are by default public and abstract)

3. Polymorphism: It means existing in more than one form. In other words, a single entity with multiple functionalities is Polymorphism.

4. Encapsulation: It is wrapping or binding data into a single unit. A single unit is nothing but a class in which we write variables and methods.

Example

public virtual class DemoClassExamp {
public static void parentMethod(){
System.debug('This is from Parent Class');
}
}
public class DemoClassExampChild extends DemoClassExamp{
public static void childMethod(){
parentMethod();
}
}

In Anonymous Window,

DemoClassExampChild.childMethod();

Benefits of OOPs in Salesforce

There are numerous benefits of OOPs, which are mentioned below.

  1. Security: We get greater security from OOPs because of data abstraction, which hides unnecessary details from the users. So, people can’t access the hidden data.
  2. Reusability: It can be achieved through Inheritance in which we need not to write the same line of code multiple times.
  3. Easy to write and test code using the OOPs approach.
  4. Readability: Code written using the OOPs paradigm is easy to read and understand.
  5. Modularity: Objects are self-contained in OOPs due to Encapsulation which makes debugging code easier.
  6. Developers can develop new Apex programs very quickly and easily since Object-oriented programming languages provide reusable code, increasing developers’ productivity.
  7. Lower cost: Due to the reusability of software, the cost of development decreases.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.