Access Specifiers in SalesForce Cloud Computing - Salesforce

Access Specifiers

  • Private: We can access inside the class only
  • Global: We can access Anywhere in apex
  • Protected: Access within the class & related classes
  • Public: We can access Inside the class & outside & related class
  • Global/public  class car {


  • Global/ public    class class name{

                                    Data members
                                    Member methods

  • Class is a blue print of real world existence
  • Class

                         |→  Features | properties | attributes
                         |→  Operators | behaviors

  • Syntax means rule / format
  • Class class name {

                  Data members
                  Member methods

  • Global class class name {

                          Data members
                          Member methods

Object: it exists in the real world
 : – it is an instance of class
Object creation syntax: –

  • Class name object name = new class name();

Creator the object for memory
Eg: – account acc = new account ();

Syntax for data member:

→ Data type name:
           Eg: integer account no;
               String lead name;

  • Keep every data member as separate



It is a set of instruction that performs a well defined task whenever it is called by its name and suitable input
→ Method is used for three purposes
1.Reusability: Eliminates replaced coding of similar task
2.Modularity: Is the decomposition of major task into sub tasks

  • Organizing entire functionality

3.To overcome limitations of operations operators
→     Unary operators  (10++ =11)
→     Binary operators  (20 > 10)
→     Ternary operators  ? ((a>b)? a:b)

Q: How to write a method ?
A:  Method has 2 parts



1.Data type of output
2.Name of the method
3.Last and sequence of input data types separated by comma (,) and enclosed in ();


Contains the operations (or) behavior
It is nothing but, we put the return type as void, when the result does not return any value.


Recommended that the body of a method consists of at least one return statement .
→ A regular normal class doesn’t have a prototype, it has definition only
→ An interface has a prototype and does not have a def
→ Method name for input  -> set values
→ If you want to display anything in Ajax use this statement , system debug (‘ ’)
→ Ex: System. debug (‘hello  Ashok’)


Class rectangle
//data members
Private integer length;
Private integer breadth;
// Member methods
Public void set Values (Integer len, Integer bth)
Length =len;
Breadth = bth;
Public void getValues ()
System.debug(‘Length=’+ Length);
→ Integer data type occupies 4 bytes of storage space in the memory
→ Programming conversions for any object oriented language.

1. Class Name:
The first letter must be capitalized, then after every word first letter must be upper case and remaining must be in lower case
Ex: Simple Interest
 Long Term Deposit

2. Method Name:
The first word lower case, then after every word first letter must be upper case.
Ex: Simple Interest
 Long Term Deposit()

3. Constant names:
Completely uppercase

4. Object name (or) data member name(or) variable name:
These are completely lower case
Ex: Simple Interest;
Long Term Deposit;
This naming conversion is known as Hungarian notation (or) coma class
Ex: Global class rectangle
Integer length;
Integer breadth;
//Member Methods
// Constructor
Public rectangle()
Length =0;
Breadth = 0;
Public void setValues(Integer  len, Integer bth)
Length = len;
Breadth = bth;
Public void getValues()
System.debug(‘length =’ + length);
System.debug (‘breadth =’ + breadth);
//Test Case
Public class Test
Public static test method void naaIshtam()
//object creation
Rectangle board = new Rectangle();


Syntax for Accessing method:
Ex: board.setvalues

→ It is a keyword
→ It is used to allocate memory depending upon the type specified after the word .

 It is a special kind of method known as constructors.


→ Special member method
→ Whose name is same as that of class name
→ Its job is to initialize values into the data members at the time of operation.
→ It does not have a return type, not even void
→ It has to be defined inside the class only.
→ It can be called only once in the lifetime of on object.
→ If you do not define it, it is supplied by the compiler and would be known as the default constructor.
State of an object: (created by constructor)

  • Current values in the data members of the object
  • Constructor provides an initial state of an object.

* No object can be created without a constructor
* Status -> AppSetup -> develop -> Ajax classes -> new

Q: What should be tested in a class?
A: code coverage
If the code coverage is not 75%, an application don’t be deployed.
It should have some name(test method)
Test Method
i/p and o/p

Converting the code into a single class:

The class which contains the test method must be opened first and it should contain the regular class which holds the real time class.
→ Define the real time class with all its data members and set member method and close it.
→ Define a test method which checks the behavior of the real time class.
→ Testclass -> Top level class
→ Rectangle -> Inner class
→ Rules for writing this type of class
→ The top level class can either be global (or) public
→ Only top level class can contain test method, but not inner class.
→ In the previous code, the object board attains a meaningful  board by visiting the class twice, which is still valid, but we can also have a mechanism which creates on objects with meaningful initial state in a single visit to the class. This is achieved through a parameterized constructor.
Public rectangle (Integer len, Integer bth)
Length =len;
Breadth = bth;
* Rectangle board = new Rectangle (6,7)
// board. setValues(6,7) //board. getValues();
Instead of this we can directory call from:
The parameterized constructor and setValues both have the same inputs and the same behavior
We can eliminate setValues and have only parameterized constructor, this statement might be wrong if both are mandatory in a class and if one can’t replace another.
The purpose of setValues is different from the purpose of parameterized constructor even through the code is same.
setValues can’t be used to give the initial state, it is only for later state and
parameterized constructor is used for the initial state and can’t be used later.
Global class TwoDpoint
Public class TwoDpoint
Private Integer x =co;
Private Integer y =co;
Public Twopoint()
x-co = x;
y – co =y;
Public Twodpoint(Integer temp)
x- co = temp;
y –co =temp;
Public void setValues(Integer X, Integer y)
x- co =x;
y-co =y;
Public void setValues(Integer temp)
x-co =temp;
y-co =temp;
Public void getValues()
System.debug(‘The point is at(‘+x-co+’,’+y-co+’));
Public static test method void main()
TwoDpoint origin = new TwoDpoint();
TwoDpoint origin = new TwoDpoint(8);
TwoDpoint origin = new TwoDpoint(12,9);
System.debug(‘origin translated to’);
Origin .getValues();
}// end of top class
→ In previous ex, there are 3 constructions, one that does not have any parameters and next one have 2 parameters and final one have only one parameter.
→ If there are no arguments in the constructor, we call it as the default constructor.
Constructors with arguments called as parameterized constructor .

Constructor Overloading:

→ When multiple constructors have the same names and different behaviors, it is known as constructor overloading and it is the first type of polymorphism

Method Overloading:

Multiple methods with same names but different signatures

Name and list and sequence of input data types

Name shadowing:
In a given situation where local variables and data members have same names gives rise to the problem called the name shadowing, in which only local variables are accessed and data members become un accessible due to naming problem
→ Local variables are declared in a method and works only on that method of that class and they are also known as object variables
→ Data members are accessible in all the methods of a class and they are known as object variables
→ The name shadowing problem can be resolved by using ‘this’.
→ ‘this’ is a keyword, which refers to the resent object.
→ ‘this’ is used to call constructor methods.
→ ‘this’ is used to call member methods.
Public rectangle (Integer length, Integer breadth)
This. Length = length;
This.breadth = breadth;
Public rectangle (Integer length, Integer breadth)
This .length =length;
This . breadth = breadth;
→ This should always be the first statement in the execution of a construction block
Q: How many decide inputs and return types of a member method are there?
→ To perform the operation of all the required values existing in the data members, do not supply any input values or else supply the required values as input
→ After performing the operation if the result is stopped in data members do not return any value (void), or else supply the data type of the calculate values as return types
→ if we do not specify any access anywhere in apex, a program that would be private access is specified by default.
           Public class account {
                                           String name;

                                           Integer ac – no;     data members
                                           Integer bal;
                                           Public account ( { 
                                           Name = ‘no name’;
                                           Ac – no =0;  à default constructor    
`                                          bal = 0;
Public account (string name, integer acc no, integer bal){  à parameterized cons
                                           this. Name = name; à object before creator
                                           this. Ac – no = ac – no;
                                           this. Bal = bal; 
Public void set values (string name, integer ac –no, integer bal){     à input
                                                 This. name = name ;
                                                 This. Ac – no = ac –no;   à after creation
                                                 This . bal = bal;
                                                 Public void deposit (integer amt){
                                                  Bal + = amt; à bal = bal +amt;

                                                 Public void withdraw (integer amt){
                                                  Bal- = amt; à  bal = bal – amt;}
                                                  Public void show balance(){
                                                  System. Debug (‘balance is = ‘ +bal);
                                                Public void  get values () {  à o/p

                                                System. Debug (‘name =’ +name);

                                                System. Debug (‘ ac – no =’ + ac – no);

                                                System. Debug(‘bal is= ‘+bal);

                                           Global class test {

                                           Public static test method void main (){

                              Account acc1= new account (‘ashok’, 100, 400000);

                                                     Acc1. Deposit (10000);

                                                      Acc1. Show Balance ();

                                                      Acc1. With draw (1500);

                                                       Acc1. Get values ();

                                                        Account acc2 = new account ();

                                                        Acc2. Sat values[‘basu’,101,50000);

                                                        Acc2. Get values();



         Enroll for Live Instructor Led Online SALESFORCE TRAINING


Get Updates on Tech posts, Interview & Certification questions and training schedules