Defining Apex Classes

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

Use the following syntax for defining classes:

private | public | global 
[virtual | abstract | with sharing | without sharing | (none)] 
class ClassName [implements InterfaceNameList | (none)] [extends ClassName | (none)] 
{ 
// The body of the class 
    
}
Note
You cannot add an abstract method to a 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. For more information about managed packages, see Developing Apex in Managed Packages.

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 Interfaces and Extending Classes.

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

© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.