Apex:
Apex is a strongly typed, object-oriented programming language that allows developers to write business logic.
One of the principal advantages of object-oriented programming techniques over procedural programming techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added.
A programmer can simply create a new object that inherits many of its features from existing objects. This makes object-oriented programs easier to modify.
Class
A class is a template or blueprint from which objects are created. An object is an instance of a class.
Suppose if we are creating 100 modules each denoting a person. Then instead of describing properties for every time we create a person, the class enables us to define common structure and create persons module by changing the properties.
A class can contain variables and methods. Variables are used to specify the state of an object, such as the object’s Name or Type. Since these variables are associated with a class and are members of it, they are commonly referred to as member variables
Syntax:
AccessModifier class ClassName{
data members;
methods;
}
To define a class, specify the following:
- Access modifiers:
- You must use one of the access modifiers (such as public or global) in the declaration of a top-level class.
- You do not have to use an access modifier in the declaration of an inner class.
- Optional definition modifiers (such as virtual, abstract, and so on)
- Required: The keyword class followed by the name of the class
- Optional extensions and/or implementations
Example 1:
Public | Global class Demo { public Integer age; public String name; }
Example 2:
Public class Employee{ Public String empName; Public Decimal Salary; Public Integer Exp; }
Syntax (with optional keywords in apex)
private | public | global [virtual | abstract | with sharing | without sharing] class ClassName [implements InterfaceNameList] [extends ClassName] { // The body of the class }
Private | This keyword can only be used with inner classes.
The private access modifier declares that this class is only known locally, that is, only by this section of code. |
Public | The public access modifier declares that this class is visible in your application or namespace. |
Global | The global access modifier declares that this class is known by all Apex code everywhere. |
With Sharing / Without Sharing | The with sharing and without sharing keywords specify the sharing mode for this class.
The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class |
Virtual | The virtual definition modifier declares that this class allows extension and overrides.
You cannot override a method with the override keyword unless the class has been defined as virtual. |
Abstract | The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined. |
Consideration for creating classes
Objects
An instance is a specific object built from a specific class. It is assigned to a reference variable that is used to access all of the instance’s properties and methods. When you make a new instance the process is called instantiation and is typically done using the new keyword.
Creating object for the Employee class (refer class examples above )
Employee E1 = new Employee ();
Statements | Purpose |
new Employee() | This allocates memory and takes copy of class variables |
E1 | This variable is of type class “Employee” takes the reference of the object created using keyword “new” and constructor. |
Using E1, now we can access class data members and methods using referencevariable dot memeber name.
E1.empName = ‘Ranjith’; E1.salary = 10000; System.debug(‘employee Name ’ + E.Name);
System.debug() : debug() is the static method belongs to inbuilt class called “system” used to write the specified message, in string format, to the execution debug log.
Use + symbol to concatenate two values.
Other real-time examples representing class and objects
Example 1:
Example 2:
Example 3:
How many ways to create Classes in Salesforce?
- Using Salesforce UI
Go to Setup
| – Develop
|- Apex Classes
- Using Force.com IDE
3. Developer Console
or
Interfaces
Interfaces includes only method signatures
The methods are not implemented in the interfaces
Interfaces can provide a layer of abstraction to your code. They separate the specific implementation of a method from the declaration for that method.
For example, a company might have two types of purchase orders, ones that come from customers, and others that come from their employees. Both are a type of purchase order. Suppose you needed a method to provide a discount. The amount of the discount can depend on the type of purchase order
An interface that defines what a purchase order looks like in general
public interface PurchaseOrder { // All other functionality excluded Double discount(); }
One implementation of the interface for customers
public class CustomerPurchaseOrder implements PurchaseOrder { public Double discount() { return .05; // Flat 5% discount } }
Another implementation of the interface for employees
public class EmployeePurchaseOrder implements PurchaseOrder { public Double discount() { return .10; // It’s worth it being an employee! 10% discount } }
When you define a new interface, you are defining a new data type. You can use an interface name in any place you can use another data type name. If you define a variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface, or a sub-interface data type.
Class Variables
To declare a variable, specify the following:
- Optional: Modifiers, such as public or final, as well as static.
- Required: The data type of the variable, such as String or Boolean.
- Required: The name of the variable.
- Optional: The value of the variable.
Use the following syntax when defining a variable:
[public | private | protected | global] [final] [static] ***data_type*** variable_name [= value]
Example
private static final Integer MY_INT; private final Integer i = 1;
Access Modifiers
Private | This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined. If you do not specify an access modifier, the method or variable is private. |
Protected | This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. |
Public | This means the method or variable can be used by any Apex in this application or namespace. |
Global | This means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the SOAP API or by other Apex code. If you declare a method or variable as global, you must also declare the class that contains it as global. |
Methods:
To define a method, specify the following:
- Optional: Modifiers, such as publicor protected.
- Required: The data type of the value returned by the method, such as String or Integer. Use voidif the method does not return a value.
- Required: A list of input parameters for the method, separated by commas, each preceded by its data type, and enclosed in parentheses (). If there are no parameters, use a set of empty parentheses. A method can only have 32 input parameters.
- Required: The body of the method, enclosed in braces {}. All the code for the method, including any local variable declarations, is contained here.
[public | private | protected | global] [override] [static] ***data_type*** method_name (input parameters) {// The body of the method}
Example of method not returning any value:
public void getInt() { System.debug(‘test display’); }
Void – denotes this method does not return any value
Example of method retuning string value:
Public String display(){ Return ‘sfdcmeet’; }
Example of method with arguments:
Public String display(String name){ Return name; }
Constructor
It is a method which has same name as class name.
This will not have any return type.
This will be invoked only once at the time of object creation.
public class TestObject { // The no argument constructor public TestObject() { // more code here } }