Apex Class Definition
public class myOuterClass {
// Additional myOuterClass code here
class myInnerClass {
// myInnerClass code here
}
}
- Access modifiers:
- You must use one of the access modifiers (such as public or global) in the declaration of a top-level class.
- You don’t 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 or implementations or both
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’s considered private. This keyword can only be used with inner classes (or with top-level test classes marked with the @IsTest annotation).
- 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 containing 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, without sharing, and inherited sharing Keywords.
- The virtual definition modifier declares that this class allows extension and overrides. You can’t 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.
A class can implement multiple interfaces, but only extend one existing class. This restriction means that Apex doesn’t support multiple inheritance. The interface names in the list are separated by commas. For more information about interfaces, see Interfaces.
For more information about method and variable access modifiers, see Access Modifiers.
Versioned Behavior Changes
In API version 61.0 and later, private methods are no longer overridden by an instance method with the same signature in a subclass. This change is versioned, so to prevent the override, update your abstract or virtual classes that contain private methods to API version 61.0 or later. In API version 60.0 and earlier, if a subclass declares an instance method with the same signature as a private method in one of its superclasses, the subclass method overrides the private method.