You are on page 1of 8

Object Oriented Programming:

============================
Apex is a Cloud-based Object Oriented Programming Language, which supports all the
Object Oriented Programming Principles.

In Object Oriented Programming, each Business Logic should be implemented in terms


of "Classed and Objects".

Class:
------
Class is a Physical Entity / Blue Print / Prototype which contains a set of
members inside it.

A class can have one or more variables, Procedures, Functions, Constructors,


Properties inside it.

By using class, we can group a set of related members together into a single
entity.

Object:
-------
Object is a Logical Entity, which holds the values for the class members.

Object is also called as an instance of the Class.

By using Object, we can reference a Class, and we can access the class
members. And We can assign the values for the class members and we can retrieve the
values from the class members.

Note:
A class can have one or more objects.

Each object contains a different set of values for the class members.

Memory will be always allocated for the objects, Not for the classes.

Each object holds the different amount of memory, as Objects are purely
supporting the Dynamic Memory Allocation. Hence we can avoid the Memory Wastage.
Ex:
SmallTalk, C++, Java, C#.Net, VB.Net, VC++.Net,... etc. , Apex.

Principles / Pillars:
---------------------
1. Encapsulation:
It allows us to group a set of related features / members together into
a single entity.

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 implementation.

Note: It is used to achieve the Data Hiding.

3. Inheritance:
Inheritance allows us to acquire the features of one class into
another, so that we can reduce the number of lines of code and we can make the
application simple and we can improve the application performance.

By using Inheritance we can achieve the "Re-Usability" and we can avoid


the Redundant Code.

4. Polymorphism:

Poly + Morphism
| |
V V
Many Forms

Polymorphism allows us to implement the multiple functions with the


same name with different signature.
Signature includes the "Number of Parameters, Type of Parameters, Order
of Parameters".

Apex Programming:
--------------
Addition(x,y) ---> x + y Addition(100,20)
Addition(x,y,z) --> Addition(100,20,234);

We have 2 Types of Polymorphism.

1. Compile Time Polymorphism / Early Binding / Static Binding:


|
|
---> Achieved by using "OverLoading".
|
--> 2 Types of OverLoading
|
--> 1. Method
OverLoading
2.
Constructor OverLoading

2. Runtime Polymorphism / Late Binding / Dynamic Binding:


|
|
---> Achieved by using "Overriding".
|
--> Done with the help of "Method
Overriding".

Class:
======
A Class is nothing but a Physical Entity / Prototype / Blue Print, which
contains a set of members inside it.

A Class can have one or more Variables, Properties, Constructors, Procedures


and Functions inside it.

Note:
We can define "N" number of Classes inside an organization. All the Compiled
Classes Code will get resides inside the "Metadata Repository".

Classes are used to implement the Business Logics.

Note: Each Apex Class Code will get resides inside a file with the extension
".apxc"

Syntax:
[Access Specifier] Class <ClassName>
{
// Define the Members..
(Variables, Properties, Functions, Procedures, Constructors,..etc)
}

Access Specifiers:
==================
By using Access Specifiers, we can define the Level of Access for each Class and
its members. (i.e. Upto What Level the Class and its members can be accessible.)

1. Private:
Private Members of the class can be accessible within the Class Only.
These can't be accessible from outside of the class.

Note:
Private Access specifier can be applicable for the "Class
Members" and for the "Test Classes".

A Normal Business Logic Class should not be defined with


"Private".

Note:
If the user didn't specified any access specifier for a class
members, then apex will treat the member as "Private" by default.

2. Public:
Public members of the class can be accessible within the Class and from
outside of the Class also from the entire organization.

Note:
A Business Logic Class should be always defined with either
"Public / Global".

3. Protected:
Protected members of the class can be accessible within the class and
from all child classes. Not from the other independent classes.

4. Global:
Global Members can be accessible within the organization and outside of
the organization also.

Note:
All the Batch Classes, Schedule Classes, WebServices, API's
should be defined with "Global".

Ways to Define the Apex Class:


==============================
1. By using "Standard Navigation":
Setup --> Build --> Develop --> Apex Classes --> New.
1. Write the Class Code inside the Editor.
2. Click on "Save / Quick Save (CTRL+S)" button.

Observation:
It will compile the class Code, and then the compiled code will
get stored inside the "Metadata Repository".

Note:
Apex Class Code will get resides inside the "ApexClass" object.
(Ex: Select id, name, body from ApexClass)

UseCase:
--------
Define an Apex Class to manage the Product Details.

Public Class ProductsInfo


{
Public integer productCode;
Public string productName, manufacturerName, location;
Public Date manufacturingDate, expiryDate;
Public Decimal unitPrice;
Public Boolean isInStock;
}

2. By using "Developer Console":

Click on your Name and Expand it.


1. Click on "Developer Console" link.
2. Click on "File --> New --> Apex Class".
3. Enter the Apex Class Name inside the "TextBox" provided by the
Dialog Box.
4. Click on "Ok" button.
5. Write the Code the inside the Editor.

Ex:
public class CustomerInfo
{
Public integer customerCode;
Public string customerName, address, emailId,
contactNumber;
Public Decimal balanceAmount, loanAmount;
Public Date joiningDate, birthDate;
Public Boolean isActive;
}

6. Save the Code by using "CTRL+S" option.

UseCase:
========
Define an Apex class to manage the Project Details.

public class ProjectDetails


{
Public integer projectCode, teamSize;
Public string projectTitle, clientName, emailID, contactNumber;
Public Decimal budgetCost;
Public Date startDate, endDate;
Public Boolean isInProgress;
}

Assignments:
============
1. Define a Class to manage the Employee Details.
2. Define a Class to manage the Student Details.

3. Define a Class to manage the Complaint Details.

Object Creation:
================
Object is nothing but an instance of a Class. By using the object, we can access
the class members and we can perform the Read / Write operations on the class
members.
(i.e. We can assign the value to the variable and we can retrieve the value from
the class variable through object)

Syntax:
<ClassName> <objectName> = new <ClassName>();

Ex:
StudentDetails sDetails = new StudentDetails();

(OR)

StudentDetails sDetails;
sDetails = new StudentDetails();

Note:
Upon creating the object, it will assign the Default Value as "Null" for each
Class member.

Note: A Class can have one or more number of objects.

Ex:
public class ProjectDetails
{
Public integer projectCode, teamSize;
Public string projectTitle, clientName, emailID, contactNumber;
Public Decimal budgetCost;
Public Date startDate, endDate;
Public Boolean isInProgress;
}

// Object Creation..
ProjectDetails pDetails = new ProjectDetails();

ProjectDetails pDetails1 = new ProjectDetails();

ProjectDetails pDetails2 = new ProjectDetails();

Assigning the Values:


=====================
By using the Object of the Class, we can assign the values for the class
members.

Syntax:
<objectName>.<variableName> = <value>;

Ex:
Public Class BranchDetails
{
Public integer branchCode, employeesCount;
Public string branchName, address;
}

BranchDetails bDetails = new BranchDetails();

bDetails.branchCode = 10001;
bDetails.branchName = 'Madhapur Branch';
bDetails.employeesCount = 7;
bDetails.address = 'Road#7, Madhapur, Hyderabad';

Retrieving the Values:


======================
We can retrieve the class member values by using the Object of the class as
below.

Syntax:
<objectName>.<memberName>;

Ex:
system.debug(bDetails.branchCode);
system.debug('Branch Name is...: '+ bDetails.branchName);
system.debug('Branch Address is...: '+ bDetails.address);
system.debug('Number of Employees....: '+ bDetails.employeesCount);

UseCase:
========
Define an Apex Class to manage the Customer Details and Assign the Values and
Print the Values.

Class Code: (Developer Console)


-----------
public class CustomerDetails
{
Public integer customerCode, age;
Public string customerName, address, contactNumber;
Public Decimal balanceAmount;
Public Date birthDate, joiningDate;
Public Boolean isActive;
}

Execution: (Execute Anonymous Window)


----------
// Creating the object of the class..
CustomerDetails cDetails = new CustomerDetails();

// Assign the Values..


cDetails.customerCode = 120045;
cDetails.Age = 29;
cDetails.customerName = 'Ram Prasad';
cDetails.contactNumber = '9900998877';
cDetails.address = 'Kukatpally, Hyderabad';
cDetails.balanceAmount = 34000;
cDetails.joiningDate = system.today();
cDetails.birthDate = Date.newInstance(1990, 02, 15);
cDetails.isActive = true;

// Retrieve / Print the Values..


System.debug('Customer Code is...: '+ cDetails.customerCode);
system.debug('Customer Age is....: '+ cDetails.age);
system.debug('Customer Name is...: '+ cDetails.customerName);
system.debug('Customer Address is...: '+ cDetails.address);
system.debug('Contact Number is....: '+ cDetails.contactNumber);
system.debug('Balance Amount is...: '+ cDetails.balanceAmount);
system.debug('Birth Date is...: '+ cDetails.birthDate.format());
system.debug('Joining Date is....: '+ cDetails.joiningDate.format());
system.debug('Customer Active Status...: '+ cDetails.isActive);

UseCase:
========
Define a Class to manage the Complaint Details.

Class Code:
-----------
public class Complaint
{
Public integer complaintNumber;
Public string status, priority, type, reason, description,
customerName;
Public Date openDate, closeDate;
Public Boolean isClosed;
}

Execution:
----------
// Creating the object of the class..
Complaint cmp = new Complaint();

// Assign the Values..


cmp.complaintNumber = 10026;
cmp.status = 'New';
cmp.priority = 'High';
cmp.type = 'Mechanical';
cmp.reason = 'Performance';
cmp.Description = 'Dear Customer Support, My WebCam is sunable to
capture the videos.';
cmp.customerName = 'Raghu Kumar';
cmp.openDate = system.today();
cmp.closeDate = system.today().AddDays(7);
cmp.isClosed = false;

// Retrieve / Print the Values..


system.debug('Complaint Number is...: '+ cmp.complaintNumber);
system.debug('Status is....: '+ cmp.Status);
system.debug('Priority is....: '+ cmp.priority);
system.debug('Type is....: '+ cmp.type);
system.debug('Reason is....: '+ cmp.reason);
system.debug('Description is...: '+ cmp.description);
system.debug('Customer Name is....: '+ cmp.customerName);
system.debug('Case Open Date...: '+ cmp.openDate.format());
system.debug('Case Close Date....: '+ cmp.closeDate.format());
system.debug('Case Closed ? : '+ cmp.isClosed);

You might also like