Define methods within a class
- Last Updated: October 30, 2020
- 6 minute read
- OpenEdge
- Version 12.2
- Documentation
Methods define the behavior of a class. A by default, a method is an instance member that is defined for access, according to its access mode, only within or through the instance of the class in which it is defined. A method can also be defined as static, which associates the method with the class type (not an instance) and makes it available, according to its access mode, throughout the ABL session without the need for a class instance. For more information on calling static methods, see Access static members.
Instance methods can access any data members, properties, and
other methods of the class including non-private (PUBLIC or PROTECTED)
data members and properties of each super class in the class hierarchy.
Method definitions are only valid in a class and cannot be defined
in a procedure. Methods also have an access mode that defines where
and how the method can be called. The possible access modes are PRIVATE, PROTECTED,
and PUBLIC, and PUBLIC is the
default. PRIVATE methods are accessible from within
the class where they are defined. An instance can access a PRIVATE method
of another instance if both instances are of the same class. PROTECTED methods
are accessible from within the class where they are defined and
in any subclass of the defining class. An instance can access a PROTECTED method
of a second instance of a class that is at the same level or in
a super class in the class hierarchy. PUBLIC methods
can be invoked by the class defining them, by any class that inherits
from that class, and by any class or procedure that instantiates
that class using its object reference. For more information on calling PUBLIC methods, see Call instance methods from outside a class hierarchy where they are defined.
A method can return a value, including a CHARACTER, CLASS, COM-HANDLE, DATE, DATETIME, DATETIME-TZ, DECIMAL, HANDLE, INT64, INTEGER, LONGCHAR, LOGICAL, MEMPTR, RAW, RECID,
or ROWID; where CLASS specifies
an object reference to a class. To return a specific value, use
the RETURN statement from within the method. A
method can also specify VOID, which means that
the method does not return a value. The code within a method is
made up of ABL statements much the same as in a procedure. Methods
can also use the syntax described in this manual that is restricted
to classes and cannot use certain syntax that is relevant only for
procedures. For example, if the method returns a value, it cannot
invoke any statement that blocks for input, such as UPDATE, SET, PROMPT-FOR, CHOOSE, INSERT, WAIT-FOR,
and READKEY. You can also raise ERROR,
from within a method using a RETURN ERROR or a UNDO, THROW,
and optionally return a RETURN-VALUE setting or
a specified error object to the caller. For more information on
handling errors returned from methods, see Raise and handle error conditions.
You can also define an abstract method prototype in an abstract class type definition, and you can define an interface method prototype in an interface type definition. A method prototype defines its name, return type, and certain other options, but no implementation. Both types of method prototype define a method that must be implemented appropriately in the class hierarchy where it is defined. For more information on defining interface method prototypes, see Define interfaces and Use the INTERFACE construct.
A subclass can define a method using the same name as a method
in its super class using the OVERRIDE option. This
new definition overrides the method of the same name and scope in
the super class. Only named methods can be overridden, not constructors
or destructors. The method name, return type, number of parameters,
and each corresponding parameter type must match between the super
class method and the subclass method. The access modes of the methods
must match or be overridden by a less restrictive access mode. For
example, a PUBLIC subclass method can override
a PROTECTED super class method. In addition, only
an instance method can override another instance method, and only
a static method can override another static method. To implement
an abstract method, you must override it with an instance method
defined in some class derived from the class that defines the abstract method.
Overriding works differently for instance and static methods.
Any class that inherits an instance method that is overridden higher
up in the class hierarchy can access only two implementations of
the method. It can call and execute the implementation that is inherited
or defined by its immediate super class using the SUPER system
reference. No other implementation of the overridden method is available
above the implementation that is inherited or defined by its super
class. Otherwise, the implementation of the method that executes
when the class calls the same method (without SUPER)
is always the most derived implementation in the instance class
hierarchy, whether that implementation is defined in the calling class
or overridden by one or more subclasses. For more information on
overriding instance methods, see Override methods within a class hierarchy.
For a static method, any override is really a re-definition of a super class implementation. A class that calls a static method always executes the nearest implementation defined in or above it in the class hierarchy. However, unlike an instance method, there can be no further override of a static method derived from the calling class, because there is no class instance to contain a subclass that can further override the method. In addition to calling the nearest implementation of an overridden static method in its own class hierarchy, a class can call other implementations of the method in any other class. Unlike an instance method of a given class hierarchy, where only the most-derived implementation executes, it is possible to directly call and execute each and every implementation of an overridden static method in the same class hierarchy, as well as in other classes that derive from the current class hierarchy. For more information on defining and accessing overridden static methods, see Define static method overrides and overloadings and Call overloaded static methods.
A class can define a method using the same name as another method defined
in the class or a super class, as long as the two methods have different signatures.
This new definition overloads any other method of the same name defined
in the class. Thus, overloading provides a means to define several methods
that perform the same function, but require different calling sequences.
The only requirement is that ABL must be able to disambiguate the signatures
of overloaded methods. ABL can distinguish many overloaded method
signatures at compile time, but it also supports certain method
overloadings that it disambiguates only at run time. Note that the STATIC option
does not participate in method overloading, so you cannot define
both an instance and a static method with identical signatures.
For more information on method overloading, see Overload methods and constructors. For more information
on how static methods participate in method overloading, see Define static members.
A method in a class can run any external procedure using the
standard RUN statement. It can also run an internal
procedure or user-defined function using a procedure object handle.
Methods are of course very similar to the internal procedures
and user-defined functions of an ABL procedure. Methods share some
of the same restrictions of internal procedures and user-defined
functions. When a method has a VOID return type,
the methods can contain any statements allowed within an internal
procedure. When the method returns a data type the method is limited
to the set of statements allowed for user-defined functions. This
limitation exists because methods with a return value can be used
in an expression, and certain statements are not allowed during
expression evaluation.
As in an internal procedure or user-defined function, local variables defined within methods are scoped to the method. Their values do not persist across method invocations but are re-initialized on every call to the method. If a local variable in a method has the same name as a data member of property for the class, the class's data member or property is shadowed by the local variable's definition. If a local variable definition in a method shadows a data member or property of the same name in its class or a super class, there is no way for the method to access the data member or property of the class.