Newer Version Available

This content describes an older version of this product. View Latest

Extended Class Example

The following is an extended example of a class, showing all the features of Apex classes. The keywords and concepts introduced in the example are explained in more detail throughout this chapter.

1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Top-level (outer) class must be public or global (usually public unless they contain
18// a Web Service, then they must be global)
19public class OuterClass {
20
21  // Static final variable (constant) – outer class level only
22  private static final Integer MY_INT;
23
24  // Non-final static variable - use this to communicate state across triggers
25  // within a single request)
26  public static String sharedState;
27  
28  // Static method - outer class level only
29  public static Integer getInt() { return MY_INT; }
30
31  // Static initialization (can be included where the variable is defined)
32  static {
33    MY_INT = 2; 
34  }
35
36  // Member variable for outer class
37  private final String m;
38
39  // Instance initialization block - can be done where the variable is declared, 
40  // or in a constructor
41  {
42    m = 'a';  
43  }
44  
45  // Because no constructor is explicitly defined in this outer class, an implicit, 
46  // no-argument, public constructor exists
47
48  // Inner interface
49  public virtual interface MyInterface { 
50
51    // No access modifier is necessary for interface methods - these are always 
52    // public or global depending on the interface visibility
53    void myMethod(); 
54  }
55
56  // Interface extension
57  interface MySecondInterface extends MyInterface { 
58    Integer method2(Integer i); 
59  }
60
61  // Inner class - because it is virtual it can be extended.
62  // This class implements an interface that, in turn, extends another interface.
63  // Consequently the class must implement all methods.
64  public virtual class InnerClass implements MySecondInterface {
65
66    // Inner member variables
67    private final String s;
68    private final String s2;
69
70    // Inner instance initialization block (this code could be located above)
71    {
72       this.s = 'x';
73    }
74
75    // Inline initialization (happens after the block above executes)
76    private final Integer i = s.length();
77 
78    // Explicit no argument constructor
79    InnerClass() {
80       // This invokes another constructor that is defined later
81       this('none');
82    }
83
84    // Constructor that assigns a final variable value
85    public InnerClass(String s2) { 
86      this.s2 = s2; 
87    }
88
89    // Instance method that implements a method from MyInterface.
90    // Because it is declared virtual it can be overridden by a subclass.
91    public virtual void myMethod() { /* does nothing */ }
92
93    // Implementation of the second interface method above.
94    // This method references member variables (with and without the "this" prefix)
95    public Integer method2(Integer i) { return this.i + s.length(); }
96  }
97
98  // Abstract class (that subclasses the class above). No constructor is needed since
99  // parent class has a no-argument constructor
100  public abstract class AbstractChildClass extends InnerClass {
101
102    // Override the parent class method with this signature.
103    // Must use the override keyword
104    public override void myMethod() { /* do something else */ }
105
106    // Same name as parent class method, but different signature.
107    // This is a different method (displaying polymorphism) so it does not need
108    // to use the override keyword
109    protected void method2() {}
110
111    // Abstract method - subclasses of this class must implement this method
112    abstract Integer abstractMethod();
113  }
114
115  // Complete the abstract class by implementing its abstract method
116  public class ConcreteChildClass extends AbstractChildClass {
117    // Here we expand the visibility of the parent method - note that visibility 
118    // cannot be restricted by a sub-class
119    public override Integer abstractMethod() { return 5; }
120  }
121
122  // A second sub-class of the original InnerClass
123  public class AnotherChildClass extends InnerClass {
124    AnotherChildClass(String s) {
125      // Explicitly invoke a different super constructor than one with no arguments
126      super(s);
127    }
128  }
129  
130  // Exception inner class
131  public virtual class MyException extends Exception {
132    // Exception class member variable
133    public Double d;
134
135    // Exception class constructor    
136    MyException(Double d) {
137      this.d = d;
138    }
139    
140    // Exception class method, marked as protected
141    protected void doIt() {}
142  }
143  
144  // Exception classes can be abstract and implement interfaces
145  public abstract class MySecondException extends Exception implements MyInterface {
146  }
147}
148
149
This code example illustrates:
  • A top-level class definition (also called an outer class)
  • Static variables and static methods in the top-level class, as well as static initialization code blocks
  • Member variables and methods for the top-level class
  • Classes with no user-defined constructor — these have an implicit, no-argument constructor
  • An interface definition in the top-level class
  • An interface that extends another interface
  • Inner class definitions (one level deep) within a top-level class
  • A class that implements an interface (and, therefore, its associated sub-interface) by implementing public versions of the method signatures
  • An inner class constructor definition and invocation
  • An inner class member variable and a reference to it using the this keyword (with no arguments)
  • An inner class constructor that uses the this keyword (with arguments) to invoke a different constructor
  • Initialization code outside of constructors — both where variables are defined, as well as with anonymous blocks in curly braces ({}). Note that these execute with every construction in the order they appear in the file, as with Java.
  • Class extension and an abstract class
  • Methods that override base class methods (which must be declared virtual)
  • The override keyword for methods that override subclass methods
  • Abstract methods and their implementation by concrete sub-classes
  • The protected access modifier
  • Exceptions as first class objects with members, methods, and constructors

This example shows how the class above can be called by other Apex code:

1// Construct an instance of an inner concrete class, with a user-defined constructor
2OuterClass.InnerClass ic = new OuterClass.InnerClass('x');
3
4// Call user-defined methods in the class
5System.assertEquals(2, ic.method2(1));
6
7// Define a variable with an interface data type, and assign it a value that is of 
8// a type that implements that interface
9OuterClass.MyInterface mi = ic;
10
11// Use instanceof and casting as usual
12OuterClass.InnerClass ic2 = mi instanceof OuterClass.InnerClass ? 
13                            (OuterClass.InnerClass)mi : null;
14System.assert(ic2 != null);
15
16// Construct the outer type
17OuterClass o = new OuterClass();
18System.assertEquals(2, OuterClass.getInt());
19
20// Construct instances of abstract class children
21System.assertEquals(5, new OuterClass.ConcreteChildClass().abstractMethod());
22
23// Illegal - cannot construct an abstract class
24// new OuterClass.AbstractChildClass();
25
26// Illegal – cannot access a static method through an instance
27// o.getInt();
28
29// Illegal - cannot call protected method externally
30// new OuterClass.ConcreteChildClass().method2();
This code example illustrates:
  • Construction of the outer class
  • Construction of an inner class and the declaration of an inner interface type
  • A variable declared as an interface type can be assigned an instance of a class that implements that interface
  • Casting an interface variable to be a class type that implements that interface (after verifying this using the instanceof operator)