Declaration of Abstract Methods and Properties
You can declare methods and properties as abstract, that is, a method or property that has a signature which specifies the parameters and results, but has no implementation in the class that defines it. The implementation may occur in one of the classes that extend the class where the method or property is initially defined.
Abstract methods and properties could be used, for example, when your application wants to provide a means for in-field customization. Your application might deliver a base class specifying the abstract methods and properties and you would allow the customization to complete those methods and properties by using a class that would extend the base class and provide implementations for some or all of the abstract methods and properties.
For a class that only contains abstract methods and properties, that is, a pure interface class, you would define a PeopleCode interface, instead of a class. You define a PeopleCode interface by using the keyword Interface instead of Class. In an interface, all the methods and properties are abstract by default.
The following example illustrates the use of abstract methods and properties. Class MyInterface is the base class which has mostly abstract methods and properties. However it is fully specified by the class MyImplementation which provides and implementation for all the methods and properties.
class MyInterface
method MyMethod1() abstract;
method MyMethod2() Returns string abstract;
method MyMethod3();
property string myproperty1 abstract readonly;
property number myproperty2 abstract;
property number myproperty3 abstract;
end-class;
method MyMethod3
end-method;
-------------------------------------------------------------------
import FOXTEST:MyInterface;
class MyImplementation extends MyInterface;
method MyImplementation();
method MyMethod1();
method MyMethod2() Returns string;
property string myproperty1 readonly;
property number myproperty2;
property number myproperty3;
end-class;
method MyImplementation
%Super = create MyInterface();
end-method;
method MyMethod1
/* dummy */
end-method;
method MyMethod2
/+ Returns String +/
Return "MyMethod2's implementation is this";
end-method;
Considerations Using Abstract Methods and Properties
Be aware of the following considerations when using abstract methods or properties.
-
You cannot have private abstract methods.
-
You will receive an error if you try to provide a method body for an abstract method.
-
The method signatures must be identical between the abstract method definition and the method implementation in the derived subclass.
-
You will receive an error if you try to provide a Get or Set body with an abstract property.
-
You will receive an error and your PeopleCode program will be terminated if you try to call an abstract method or property at runtime, unless caught in a try-catch block.
-
The class that implements an interface must still assign %Super in its constructor.