Classes Salesforce
Classes Salesforce
Explain constructors
Explain constants
Explain inheritance
Demonstrate how to use with and without
sharing keywords
An Apex Class is a template or blueprint from which objects are created. It is similar to a Java class and have
variables and methods. Variables can contain the data and methods have the logic that act upon that data.
Class
methods attributes
Let’s understand the basic syntax of an Apex class. To define an Apex class, you need to specify a couple of
keywords per the syntax given. Let’s understand each keyword and its use.
//inner class
class myFirstInnerClass {
class mySecondInnerClass {
}
}
Apex variables are of a specific data type and contain the data on which logic is performed. Apex class contains:
variables and methods. Variables in an apex class are also called class variables as they belong to the whole
class. Individual methods in an Apex Class can also contain their own variables.
//apex class
public class newCarModel{
//class variable
public static String model = '';
//method
public void drive(){
//method variable
Integer speed = 50;
//class variable can be accessed in the method
system.debug(model);
}
}
©Simplilearn. All rights reserved 9
Apex Class Variable Syntax
• Definition modifier
• Data type of the variable
• Appropriate name to the variable
• Value to the variable
Apex Class Methods are defined in an Apex Class that provide logic to accomplish a particular task. Let’s
examine the syntax of a method.
//public method with Integer as return type //public method with Integer as return type
system.debug(k);
Double c = a + b;
return c;
Protected }
} }
} }
} }
} }
} }
} }
A Constructor is a block of code that initializes class variables and set them to default values. This is important
as there might me functionality in class methods that depend on the initialization of class variables and their
default values.
public MyClass () {}
must have a different argument number
// A constructor with one argument
and type. public MyClass (Boolean call) {}
Creating a new object from a Class is called Object Instantiation. To work with classes, non-static methods,
and attributes, we need to instantiate objects from a class using one of the constructors defined in the class.
Once you have instantiated an object, you can refer to methods and attributes using dot notation.
classObject.method1();
classObject.myVariable = 'Test';
Static is a definition modifier. Variables and Methods both can be declared as static. Static methods and
attributes have the following characteristics:
Constants are variables whose values do not change once they are initialized. They can be declared using
static and final keywords. The variable with these two keywords indicates that the variable can only be
assigned once, either in the declaration or with a static initializer method.
return 2 + 7;
static {
PRIVATE_INT_CONST2 = calculate();
KNOWLEDGE
public PersonClass(String firstName, String lastName, Integer phone){
CHECK /constructor code
}
}
KNOWLEDGE
public PersonClass(String firstName, String lastName, Integer phone){
CHECK /constructor code
}
}
The data type of the values being passed should match the constructors arguments data type.
//This constructor calls the first constructor using the this keyword.
public testThis() {
this('None');
When a class inherits the functionality of another class, instead of creating the variables and methods again,
re-use another class’s functionality that is called Inheritance. This class can also have variables and methods
of its own.
//class declared as virtual class so that it can be overridden // This class extends Car class
//virtual method can be overridden as well //method that overrides parent class’s method
} }
return 100;
Car obj1, obj2;
}
obj1 = new Car();
}
// This outputs 'This is a car.'
obj1.carType();
obj2.carType();
Integer i = obj2.topSpeed();
• without sharing {
// Code here
// Code here
}
Interfaces are similar to classes but their methods are not implemented; only method signatures are defined,
but the body of each is empty. The main advantage of Interfaces is that they provide a layer of abstraction to
your code.
// An interface that defines what a purchase order looks like in general
Double discount();
// One implementation of the interface for customers // Another implementation of the interface for employees
public class CustomerPurchaseOrder implements PurchaseOrder { public class EmployeePurchaseOrder implements PurchaseOrder {
return .05; // Flat 5% discount return .10; // It’s worth it being an employee! 10% discount
} }
} }
• Salesforce Platform is a multi-tenant environment, due to this, the Apex runtime engine enforces certain
limits.
• Apex runtime is the engine that handles Apex code on Salesforce Platform.
• These limits are enforced by Apex runtime to ensure that no one is taking up excessive Platform
resources.
• Limits Methods are there to avoid exceptions, which check the resources consumed and the amount still
available.
• Salesforce Developer must always uses these Limits methods in Apex code as that might run into
platform limits and exceptions can occur.
Version Example
Returns the amount of platform Limits.getHeapSize()
resource that has been used so far in
the current transaction
Contains the word “Limit” returns the Limits.getLimitHeapSize()
total amount of the resource available
getHeapSize Returns amount of memory (in bytes) used for the heap
This is the right syntax. Using this keyword, we can call a contructor from another constructor.
b. PersonClass.personNameID();
b. PersonClass.personNameID();
personNameId is a static method, hence it can only be called using className and dot notation.
a. System Context
b. User Context
a. System Context
b. User Context
secondClass does not have any with or without sharing keyword declared on it. So the code will follow the
context of the calling class, that is, firstClass which is running in user context.
c. Date d = System.now();
d. DateTime dt = System.today(now);
c. Date d = System.now();
d. DateTime dt = System.today(now);
Although, the debug statement will not write the str variable's value, it will not throw an error.