When defining methods and variables in Apex, you have the option of using the private, protected, public, or global access modifiers. In this blog post, we'll look at the types of access specifiers used in Apex programming.
Global/public class car {
DM
MM
}
Global/ public class name{
Private
Data members
Public
Member methods
}
If you would like to become a Salesforce Certified professional, then visit Mindmajix - A Global online training platform: " Salesforce Certification Training Course ". This course will help you to achieve excellence in this domain.
Class is a blueprint of real-world existence
|→ Features | properties | attributes
|→ Operators | behaviors
Class name {
Data members
Member methods
}
Global class name {
Private
Data members
Public
Member methods
}
Object: it exists in the real world
: – it is an instance of the class
Example
Object creation syntax: –
Creator the object for memory
Eg: – account acc = new account ();
Syntax for data member:
→ Data type name:
Eg: integer account no;
String lead name;
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
3.To overcome limitations of operations operators
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
Body
{
Return
}
Void:
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 a 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’)
Ex:
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);
System.debug(‘Breadth=’+Breadth);
}
}
→ 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 the 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
Ex: SIMPLE INTEREST
LONG TERM DEPOSIT()
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;
}
//Input
Public void setValues(Integer len, Integer bth)
{
Length = len;
Breadth = bth;
}
//output
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();
Board.getValues();
Board.setvalues(6,7);
Board.getValues();
}
}
Syntax for Accessing method:
Object.memberMethod
Ex: board.setvalues
New():
→ It is a keyword
→ It is used to allocate memory depending upon the type specified after the word .
Rectangle():
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)
[ Related Article:- Interview Questions and Answers of Salesforce ]
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)
Rectangle
{
}
Test
{
}
TestClass
{
Rectangle
{
}
Test Method
{
i/p and o/p
}
}
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.
Ex:
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);
Origin.getValues();
Corner.getValues();
Top.getValues();
Origin.setValues(6,2);
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.
→ When multiple constructors have the same names and different behaviors, it is known as constructor overloading and it is the first type of polymorphism
Multiple methods with same names but different signatures
Signature:
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.
Ex:
Public rectangle (Integer length, Integer breadth)
{
This. Length = length;
This.breadth = breadth;
}
Ex:
Public rectangle (Integer length, Integer breadth)
{
This();
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.
Ex:
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();
}
}
[ Related Article:- Learn Salesforce Tutorial ]
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Salesforce Training | Nov 19 to Dec 04 | View Details |
Salesforce Training | Nov 23 to Dec 08 | View Details |
Salesforce Training | Nov 26 to Dec 11 | View Details |
Salesforce Training | Nov 30 to Dec 15 | View Details |
Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.