top of page

OBJECT ORIENTED PROGRAMMING(OOPS) IN SALESFORCE

  • Writer: Abhilash Banpurkar
    Abhilash Banpurkar
  • Nov 28, 2022
  • 5 min read

Updated: Feb 11, 2023

What is this ?

It is a concept or It is Collection of Principles or it is one kind of Approach or it is a paradigm, These principles or concept was developed by Mr. Grady Booch. So we can call him as Father of OOPs.


What are these principles:


1. Encapsulation

2. Abstraction

3. Polymorphism

4. Inheritance


Let Us Discuss about OOPs Principles:

1.Encapsulation:

Binding of Programming Elements(PE) i.e Data and Instruction in a single unit.

(OR)

Binding of STATE(DATA/PROPERTY) AND BEHAVIOR(FUNCTIONALITY)as single unit is known as ENCAPSULATION. (DATA+FUNCTIONALITY)




We can implement Encapsulation by using 2 Keywords in Apex.

1. Class

2. Interface



Class:



It is a blue print or it is a drawing of an object means how object is look like and how object is represented that we can do by having a class.


1) It is a blue print of real world existence.(or)

2) It is a collection of features and behaviors. (or)

3) It is a collection of data members and member methods. (or)

4) It is a user defined data type.


We identify something in this world by its features and behaviors.

We can denote Features as Data and we can denote Behaviors as Methods.


Syntax of Class:



                 Class <Class name>
                     {
                        Data Members;  --  Features
                         Member Methods;    -- Behaviors 
                      }

Eg:


      Class Student
          {
                 Integer sno,mbno,m1,m2,m3,tm;
               String sname;
               Decimal  fees,am;
                  Total_Marks();
                 Avg_Marks();
        }

.



Member Methods: It is of 4 types.

1. Create

2. Input

3 Process

4. Output.


Every member method gives some service to data members. If any member method does not give any service to data members, then there is no point of writing the member method with in the class.


Interface: It contains Method Prototypes only.



Object:


It is a real world entity, means what we can see and what we can touch, feel is known as Object such as, Fan, Chair etc.


Every object has some STATE and BEHAVIOUR ,State means Properties (data) and Behavior means Functionalities (methods).


For ex:


Take a Class called HUMAN From Human class we can derive two objects i.e MEN and WOMEN.

MEN properties like Name, Height, Weight, Color etc.,

Behavior like eat(),walk(),Write(), Read() etc.,

So same kind of properties and behavior for Woman also but there are some different functionalities between MEN and WOMEN i.e like

MEN cannot give the birth to a child but where are WOMEN can.


Another example:

Every MEN and WOMEN have 2 legs, 2 hands, 2 eyes,2 ears,1 nose, 1 mouth this is common parts for both MEN and WOMEN some of the parts of the will differ from MEN and WOMEN. This is property differences.


Definition of an Object:


1. It is an instance of a Class

2. It is a real world existence of class

3. It is a variable of type class


How to create an object:


Dataype name;

Classname objectname = new classname();

1 2 3 4

Here

1. classname – represents-- data type

2. objectname – represents – variablename

3. new= dynamic memory allocation .

object always allocated memory through dynamic memory allocation.

What is memory allocation:

Allocation means reservation, reserve what? i.e main memory (RAM)

We can allocate the memory in 2 ways


1. Static Memory Allocation: Memory will be allocated at the compilation time or translation time.


Suppose we written this statement

Datatype variablename;

Integer a; --- > static memory allocation

Suppose we wrote a program of 75 lines

5 10 15 20 25 30 35 40 45 50 55 60 65 70

All these statements have the following statement

Data Type variable name; à this statement we written in above mentioned lines

First this program need to translate into machine language

And starts the execution—just before start the execution it allocates memory and then starts the execution. This is called Static memory allocation.



2. Dynamic Memory Allocation:


Datatype variablename = new datatype();

When we create any object then it always it is dynamic

4.classname()—constructor name


note: class name and constructor name must be same.



what is constructor:



1.It is special member method, because classname and constructor name must same.

2. It is called only once in the life time of an object, when the object is created.

3. It is always preceded by new operator.

4. Its job to load the values into data members when object gets created( initialize the values to data members).

5. It does not have any return type not even void


Who will execute the Apex code ?

ARE—Apex Runtime Engine will take the responsibility to execute the Apex code

Whatever code we written, that code will talk to ARE and ARE will execute the Code.

ARE has to start the execution, in Apex this main requires which we need to write it as ‘testMethod’

We need to write like this

Public static testMethod void main() – this is the starting point of execution of Apex program.


2) ABSTRACTION:


It means only showing the required things to the outside world so that we can hide the inner details. Levels of Hiding the Data and Methods which are Encapsulated.

We can implement Abstraction by using Following 4 keywords

1. Private.

2. Public.

3. Protected.

4. Global.


Private: The members with this abstraction are visible and accessible only with in the Encapsulation.


Public: The members with this abstraction are visible and accessible throughout the organization.


Protected: The members with this abstraction are visible and accessible not only with in the encapsulation and also in related encapsulations (Inheritance concept).


Global: The members with this abstraction are visible and accessible throughout the Salesforce.com


Note:

1) Before data member and member method we have to specify abstraction.

2) At the Encapsulation level, as per the oops principles, Data must be hidden and Methods must be visible. So data must be Private and methods must be Public.


Syntax:

Abstraction Data member;

Abstraction Member Method();


Example:


 Class Student
{
Private  Integer sno,tm;
Private  String sname;
Private   Decimal avg;
Public   Total_Marks();
Public    Avg_Marks();
}


3).POLYMORPHISM : Here POLY means Many, MORPH means Forms.

For ex: Restaurant, Functions, we can implement Polymorphism using following ways.


1. Method OverLoading.

2. Method Overriding : Generally we are walking in the direction of our face, but if somebody asking to walk backward direction we can walk, but it is in different direction. So, When we change the mechanism of the function is called overriding.

3. Operator OverLoading

4. Constructor Overloading



4) INHERITANCE: Reusing the Data and Functionality in one Encapsulation for another Encapsulation.


Example:


 Class Human
{
// Data Members
String Name, Color;
Private Integer Age, ,Height, Weight;
// Member methods
Public Walk();
Public Talk();
Public Sleep();
}

We can derive two sub classes i.e., Male Class and Female Class and every Male and Female Class.

Have same kind of data and method so that we no need to declare once again we can directly inherit from Human Class into Male and Female class. This is called Inheritance.


So, these are the 4 Key pillars of OOPs.


There are 3 places we can write the code, out of which 2 are cloud based and 1 are desktop based




Structure of Apex program:


1) Every Apex program must be a class /Encapsulation

2) Apex Classes are classified into 2 types


We can write 2 types of classes in Apex


1. Business Class: this Class is used write to develop the application, and whatever definitions we discussed earlier that is fit into this class, like blue print, user defined data type etc.,


2. Test Class: this class is used to write for test the behavior of business class.


First we need to discuss with how to write Test class.:


 Test Class name is Demo Test
@isTest
  Public  class DemoTest{
 }

Note: to distinguish between the test class and business class, test class always need starts with ‘@isTest’.

Test class we can execute but business class we cannot execute. Business class will be executed via test class.

To save and compile the test class or business class we need to type ctrl +S.


How to write a business class:



   Public class  ClassName{
           Data members;
           Member methods;
    }

Note: first always we need to write business class then we need write test class.



 
 
 

Recent Posts

See All

Comentarios


bottom of page