Method scoping within a class hierarchy
- Last Updated: February 17, 2022
- 2 minute read
- OpenEdge
- Version 12.2
- Documentation
A PRIVATE method
can be called by the defining class. An instance can call the PRIVATE method of another instance if they are both from the same class. A PROTECTED method can be called by the defining class and any of its
derived classes. A PACKAGE-PRIVATE method can be called from
within the class and any class within its package. A PACKAGE-PROTECTED method can be called from within the class, any class within its
package, and from within any subclass that inherits the class. A PUBLIC method can be called from any code (in a class or a procedure) that has
access to a class instance. The default access mode is PUBLIC. When declaring an interface method prototype, the access mode for this
method must be PUBLIC (the default). When defining an
abstract method, the access mode for the method cannot be PRIVATE.
Existing A class can access all of the PUBLIC and PROTECTED methods of its super class
as well as any methods that it defines. A super class that, in turn, inherits from another
class inherits all its super class's PUBLIC and PROTECTED methods and so on up to the root of the hierarchy.
Therefore when a class inherits all of the PUBLIC and
PROTECTED methods of its super class, it inherits all of
the PUBLIC and PROTECTED
methods available all the way to the top of the class hierarchy.
While a subclass can access the PUBLIC and
PROTECTED methods of any of its super classes, the
reverse is not true for instance methods. You cannot invoke instance methods within a class
that are defined only by a subclass of that class. Methods first defined in a subclass are
simply not visible to any of its super classes, even though the subclass exists in the same
class hierarchy as its super classes for a given object. Likewise, methods defined as
PRIVATE in a super class are not visible to its
subclasses. Because the default access mode for methods is PUBLIC, a method is always accessible to a subclass unless you specifically
define it as PRIVATE. Therefore, a key benefit of classes
is the PROTECTED access mode, which allows you to define an
interrelated set of method definitions that are accessible within a class hierarchy, but
which are invisible to any procedures or other classes that are outside that class
hierarchy.
Note that an instance can call a PROTECTED
method of a second instance that is at the same level or in a super class in the class
hierarchy, and an instance can call a PRIVATE method of
another instance if both are the same class.
PACKAGE-PRIVATE and
PACKAGE-PROTECTED access modes are another level of access control for
methods. For more information see Access modes.