1. Classes
2. Properties
3. Encapsulation
4. Polymorphism and Inheritance
5. Virtual Class
6. Abstract class
7. Interfaces
Classes
- A class is a template or blueprint from which objects
are created.
- They consist of methods and attributes
- Are stored with the version of API that is used to
compile it
- May contain other classes, known as inner classes (but
these can only be one level deep)
- Even though Apex code is not case sensitive, it is
recommended that you follow the Java naming convention
- Static methods and attributes can only be declared in a
top-level class definition
- To create new exception classes, the Exception class
must be extended
- Classes can be enabled or disabled for profiles.
- Through the UI:
- Go to Setup -> Develop and Apex Classes.
- Click “New” or “Generate from WSDL”
- Enter code or upload a WSDL
- Through a Force.com IDE project:
- Right-click the src folder and click New -> Apex
Class (this is the recommended way)
Class Syntax:
private | public | global
[virtual | abstract | with
sharing | without sharing | (none)]
Class ClassName [implements InterfaceNameList | (none)] [extends ClassOrInterfaceName | (none}] {
// The body of the class
}
Class ClassName [implements InterfaceNameList | (none)] [extends ClassOrInterfaceName | (none}] {
// The body of the class
}
Example
public class myOuterClass {
// Additional myOuterClass
code here
class myInnerClass {
// myInnerClass code here
}
}
- If global, the class is accessible everywhere. This must be used
with the webService keyword. All methods, attributes and inner classes
that are global must be within a global class
- If public, the class is visible across the application, org or
namespace that comprises the class.
- If private is used for an inner class, the inner class is only
accessible to the outer class. The default for inner classes is private.
- Top level or outer classes must have either a global or public
keyword.
- A class can implement multiple interfaces, but it can only extend
once class
- A class can cast as a superclass and verify an objects class using
the instanceof keyword
- You can implement and extend classes using the keywords, virtual,
abstract and extends
- Virtual declares that the class allows extensions and
overrides, Classes that are virtual cannot be global.
- Abstract declares that the class contains abstract
methods and can be extended. These classes just have a method signature
and do not have code. You cannot instantiate an object of an abstract
class until some other class extends it.
- The Extends keyword declares that the class is a
subclass. The “super” keyword can be used to invoke constructors and
methods from the parent class.
- All Apex code executes in the system mode and ignores all CRUD,
field-level security and record sharing level privileges. You use with and
without sharing keywords to implement sharing. By default a class will run
in the without sharing mode.
- With sharing – This means that when performing DML
operations, the user can only update records to which he or she has edit
level access
- Without Sharing – This ensures that sharing model
access is ignored and is referred to a running in system mode.
- Interfaces are classes that only include the method signature. The
methods are not implemented in the interfaces. Apex supports both
top-level and inner interfaces
- Avoid using standard object names for class names. Doing so causes
unexpected results
Properties/ Attributes Syntax
Properties
are the shortened form of a method that has access to static and instance
variable.
modifiers dataType attributeName
initialization;
For example:
public static final Integer MAX_AMOUNT = 200;
public static final Integer MAX_AMOUNT = 200;
public
Integer getcost() { return cost; }
public
void setcost(Integer cost) { this.cost=cost; }
public
Integer cost { get { return cost; } set { this.cost= cost;}}
public
Integer cost { get; set; }
//read-only
and write - only Property
public
Integer readOnly { get; private set;}
public
Integer wrtieOnly { private get; set;}
Methods Syntax:
Modifiers returnDataType
methodName (inputParameterList) {
// method code
}
// method code
}
For example:
Public Integer getInt() {
Return myInt;
}
Return myInt;
}
Method and Attribute
Access Modifiers
- Private – Default access modifier, but is not available for
top-level classes. It implies that the attribute or method is only
available in the class where it is defined.
- Protected – Available only to inner classes
- Public – Can be used by an apex code in the application, org or
namespace
- Global – Accessible by all Apex everywhere. Note that Apex code
cannot be shared between orgs, with the exception of Web Services, which
are accessible to everyone.
Static Methods and Attributes
- Static methods are accessed through the class itself and do not
depend on an instance of a class
- Static attributes are used to store data that is used within the class. They can be used to prevent recursive logic by setting flags.
- Assign a value to a constant only once either at declaration or
initialization
- Define a constant using both the static and final keywords.
Instantiating Objects
- Allows you to work with methods and attributes that are not defined
as static
- After instantiating, you can refer to methods and attributes using
the dot notation. For example:
TestObject myObject1 = new TextObject();
myObject1.myMethod();
myObject1.myMethod();
- A constructor is a special method used to create an object of a
class. It has the same name as the class and is the first method invoked
in the class. It does not have an explicit return type and is available by
default in each class as invisible and without parameters. It can be
overloaded by defining multiple constructors with different parameters.
The “this” Keyword
- With dot Notation – Used to represent the current
instance of the class and can call methods or set attributes that are
public or are available.
- With Constructors – Developers can do constructor
chaining by using the this keyword, but the this keyword must be the first
statement in the constructor.
Apex System-Delivered Classes
- System Class – Is a static class that contains only
static methods. Includes the following:
- Debug()
- Now()
- Today()
- Assert()
- AssertEquals()
- AssertNotEquals()
- UserInfo Class – Mostly getter methods used to get
details. For example:
- getUserId()
- getUserName()
- getUserRoleId()
- getFirstName()
- getLocale()
- getLanguage()
Apex Workflow and Approval Processing
- Apex process classes are used to submit workflow
requests and process the results of those requests. The three classes
provided are:
- ProcessRequest – Used to process the results from a
workflow process
- ProcessSubmitRequest – Used to submit a workflow item for
approval
- ProcessWorkItemRequest – Used to process an item after
it is submitted
Encapsulation
When a class is defined it becomes a new
data type in Apex
public class MyClass()
{
MyClass c = new
MyClass();
List<MyClasss>
myList = new List<MyClass>();
myList.add(c);
}