Apex Class Definition

In Apex, you can define top-level classes (also called outer classes) as well as inner classes, that is, a class defined within another class. You can only have inner classes one level deep. For example:
public class myOuterClass {
   // Additional myOuterClass code here
   class myInnerClass {
     // myInnerClass code here
   }
}
To define a class, specify the following:
  1. 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.
  2. Optional definition modifiers (such as virtual, abstract, and so on)
  3. Required: The keyword class followed by the name of the class
  4. Optional extensions and/or implementations
Note

Note

Avoid using standard object names for class names. Doing so causes unexpected results. For a list of standard objects, see Object Reference for Salesforce and Force.com.

Use the following syntax for defining classes:

private | public | global 
[virtual | abstract | with sharing | without sharing] 
class ClassName [implements InterfaceNameList] [extends ClassName] 
{ 
// The body of the class
}
  • The private access modifier declares that this class is only known locally, that is, only by this section of code. This is the default access for inner classes—that is, if you don't specify an access modifier for an inner class, it is considered private. This keyword can only be used with inner classes.
  • The public access modifier declares that this class is visible in your application or namespace.
  • The global access modifier declares that this class is known by all Apex code everywhere. All classes that contain methods defined with the webService keyword must be declared as global. If a method or inner class is declared as global, the outer, top-level class must also be defined as global.
  • The with sharing and without sharing keywords specify the sharing mode for this class. For more information, see Using the with sharing or without sharing Keywords.
  • 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.
  • The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined.
Note

Note

  • You cannot add an abstract method to a global class after the class has been uploaded in a Managed - Released package version.
  • If the class in the Managed - Released package is virtual, the method that you can add to it must also be virtual and must have an implementation.
  • You cannot override a public or protected virtual method of a global class of an installed managed package.

For more information about managed packages, see What is a Package?.

A class can implement multiple interfaces, but only extend one existing class. This restriction means that Apex does not support multiple inheritance. The interface names in the list are separated by commas. For more information about interfaces, see Understanding Interfaces.

For more information about method and variable access modifiers, see Access Modifiers.

Previous
Next