Object Oriented Programming
Object Oriented Programming
============================
In Procedure Oriented Programming, each business logic should be implemented in-
terms of "Procedures and Functions". Which causes the below drawbacks.
1. In Procedure Oriented Programming, all the functions are moving around the
application.
(i.e. One function can call the other functions inside the application)
2. We can't define the level of access for the functions. (i.e. We can't
specify, up to what level the members can access)
i.e. We are grouping a set of related methods into the smaller units called as
"Classes". To access the class members, we need a reference called as an "Object".
Class:
------
Class is a Physical Entity / Blue Print, which contains a set of members
inside it.
Note:
All the apex classes information will get resides inside the
"ApexClass" object.
Ex:
Select id, name, body from ApexClass
Object:
-------
Once the class has been defined with the required members inside it. In-order
to access members we need a reference called as an "Object".
Note:
Always memory will be allocated for the "Objects", because it holds the
values for the class members.
A class can have one or more objects. Each object contains its own memory
location.
Each object can store a different value for the class member.
Note:
Object is also called as an "Instance Of a Class".
Each object should have a unique name, through which we can reference the
class members.
Note:
Objects are purely supporting the Dynamic Memory allocation, which
provides the efficient utilization of memory.
Note:
For objects memory will be allocated inside the "Heap Memory".
1. Encapsulation:
Encapsulation is used to group / bind a set of variables and functions
together into a single unit.
Note:
Encapsulation can be achieved with the help of "Classes and
Interfaces".
2. Abstraction:
It provides the user interfaces to the users, but without providing the
complexity of the implementation.
Note:
By using Abstraction, we can achieve the "Data Hiding".
3. Inheritance:
By using this feature, we can acquire / access the features of one
class into an another. So that we can avoid the redundancy. So that we can make the
code generic.
Note:
By using Inheritance we can achieve the "Re-Usability".
4. Polymorphism:
By using this feature, we can create multiple functions inside the
class with the same name with different signature.
Class:
======
Class is a Physical Entity, which contains the business logic, to be get referenced
inside the application.
Syntax:
<Access Specifier> [With/Without Sharing] Class <ClassName>
{
// Define the Class Members..
...
...
}
Note:
1. Always class name should be starts with a "Character".
2. Class Name should be always a single word.
3. Class Name should be always "Unique". i.e. We can't define 2 classes with
the same name.
4. Compiled Apex classes information will get resides inside the "MetaData
Repository".
Access Specifier:
-----------------
Access Specifier is used to indicate the level of the access for the class and the
class members.
It can be applicable for both Class and its members. We can have a different access
specifier for each member inside the class.
1. Private:
Private access specifier can be applicable for the class members (i.e.
Variables, functions, properties, etc) and "Test Classes".
Private members of the class can be accessible from within the class
only. They can't be accessible from outside of the class.
Note:
If the user didn't specify the access specifier for a class
member. Then apex will treat the member as "Private" by default.
2. Public:
Public members can be accessible within the class, and from outside of
the class within the entire organization.
3. Protected:
This can be applicable for the class members (like Variables,
properties, methods, etc). It can't be applicable for the classes.
Protected members of the class, can be accessible within the class and
from all the associated child classes.
4. Global:
Global members can be accessible within the organization and outside of
the organization also.
Note:
Every Batch Class (Batch Apex), Schedule Class (Schedule Apex),
Future Methods, Webservices, and API's should be defined with "Global Access
Specifier".
Note:
A Class should be defined with either "Public / Global".
/*
Define an apex class to manage the Employee Details.
*/
/*
Define an apex class, to manage the Project Details.
*/
Object:
It is nothing but an instance of a class, which is used to assign / retrieve
the values from the class members.
Syntax:
<ClassName> <objectName> = new <ClassName>();
(OR)
<ClassName> <objectName>;
<objectName> = new <ClassName>();
Ex:
Employees emp = new Employees();
Note:
Upon creating the object, apex will assign the "Null" value for each variable
by default.
Ex:
ProjectDetails pDetails = new ProjectDetails();
(OR)
ProjectDetails pDetails;
pDetails = new ProjectDetails();
Note:
We can create multiple objects for the same class. But object names should be
always unique.
Ex:
Employees emp = new Employees();
By using the Object of the class, we can assign the value to the class variables
and we can retrieve the values from the class variables.
Syntax:
<objectName>.<memberName> = <value>;
Ex:
Employees emp = new Employees();
emp.empId = 100001;
emp.empName = 'Suresh Kumar';
emp.salaryAmount = 45000;
emp.joiningDate = system.Today();
emp.isActive = true;
Ex:
system.debug(emp.empId);
system.debug('Employee Name is...: '+ emp.empName);
system.debug('Address is....: '+ emp.address);
system.debug('Salary is...: '+ emp.salaryAmount);
/*
Create an apex Class, to manage the Student Details. And assign the values
and display the values.
*/
Class Code (Developer Console):
----------
public class StudentDetails
{
public integer studentID;
public string studentName, address;
public decimal feeAmount;
public boolean isActive;
public date birthDate, joiningDate;
}
/*
Create an Apex class, to manage the ProjectDetails. And Assign the values and
Display the values.
*/
Class Code:
-----------
public class ProjectInfo
{
public integer projectCode;
public string title, clientName, country;
public Date startDate, endDate;
public decimal budget;
}
Execution:
----------
// Create the object of the class..
ProjectInfo pInfo = new ProjectInfo();
ORM Technology:
===============
ORM stands for "Object Relationship & Mapping".
Salesforce purely supports the ORM features, which provides all the 3 layers of the
application upon creating an object inside the organization.
i.e. Upon creating the object, salesforce provides the below 3 layers.
1. It creates a Table.
2. It creates an User Interface (Tab)
3. It creates the Business Logic (Class)
Ex:
Object Name : Customer
Table Name : Customer__c
User Interface (Tab) : Customers
Business Logic(Class): Customer__c
i.e. Upon creating an object, salesforce provides the required business logic
in-terms of a class, which will be having the same name like as the Table Name.
Note:
Each object in salesforce(Standard / Custom) will be represented as a Class.
Ex:
Account Object will represented as an Account class..
Note:
Each field inside the object will be represented as a variable inside the
associated class.
Ex:
Account object fields will be represented as below..
// Pagination Methods..
}
Note:
By using these classes we can interact with the objects through
programatically, and we can perform all the DML operations on the object records.
Note:
Hiring Manager Object will be represented in-terms of a class as below..
Note:
To insert the records into the associated objects through apex programming,
we have to follow the below steps.
/*
Write an apex program, to insert an Account record into the object.
*/
// Step 1: Create the object of the class..
Account acc = new Account();
ld.firstName = 'Suresh';
ld.LastName = 'Tripathi';
ld.Company = 'Infosys';
ld.status = 'Open - Not Contacted';
ld.Rating = 'Hot';
ld.Industry = 'Banking';
ld.phone = '9900554433';
ld.fax = '9900887766';
ld.email = 'suresh@gmail.com';
ld.city = 'Hyderabad';
ld.street = 'Kukatpally';
ld.State = 'Telangana';
ld.PostalCode = '500031';
ld.Country ='India';
insert ld;
/*
Write an apex program, to insert a Hiring Manager Record into the object.
*/
insert hr;
if(hr.id != null)
{
system.debug('Hiring Manager Record has been inserted Successfully. Id
is..: '+ hr.id);
}
Assignments:
============
1. Write an apex program, to insert a Case Record into the object.
2. Write an apex program, to insert a Contact Record into the Contact Object.
4. Write an apex program, to insert a Position Record into the Position object.
We can create both Parent and Child records into the objects, which are associated
with any relationship (either Lookup / Master-Detail).
Upon creating the associated records, we need to follow the below steps.
5.1. Upon creating the child record, supply the Parent record id to the
Relationship Field.
6. Insert the Child record into the object, by using "Insert" Statement.
/*
Write an apex program, to Insert an Account Record. And an associated
Contact record into the object.
insert acc;
if(acc.id != null)
{
system.debug('Account Record Inserted Scucessfully. Account id is..: '+
acc.id);
con.firstName = 'Child';
con.lastname = 'Record 1';
con.title = 'Project Manager';
con.Phone = '9988000000';
con.fax = '9988776666';
con.email = 'record@gmail.com';
con.mailingCity = 'Bangalore';
insert con;
if(con.id != null)
{
system.debug('Contact Record Created. Record Id is...: '+ con.id);
}
/*
Write an apex program, to insert a Hiring Manager Record, and an associated
Position record into the object.
insert hr;
if(hr.id != null)
{
system.debug('Hiring Manager Record Id is...: '+ hr.id);
insert pos;
if(pos.id != null)
{
system.debug('Position Record ID is...: '+ pos.id);
}
}
Assignments:
============
1. Write an apex program, to insert an Account Record. And an associated Case
Record.
Hiring Manager
|
--> Position
|
--> Candidate
4. Create a Lead Record into the Object. And an associated "Task Record".
Syntax:
For(integer counter = 1; counter <=10; counter++)
{
// Write the code to insert the record.
}
/*
Write an apex program, to insert 100 Hiring Manager records into the object.
*/
insert hr;
if(hr.id != null)
{
system.debug('Hiring Manager Record Id is...: '+ hr.id);
}
}
Note:
Upon performing the DML operations through the Apex Programming, we can
perform max. of 150 DML statements inside a single transaction.
When the user tries to use more than 150 DML statements inside the
transaction, then salesforce will cross the governor limits. And will abort the
transaction and the previous operations will get RolledBack.