Axaptapedia is now maintained by AgileCadence For more information please click here.

Access modifiers

From Axaptapedia
Jump to: navigation, search

Method access modifiers are a means of restricting access to a certain method of a class as required.


There are three levels of access available:

  • public
  • protected
  • private

To understand the use of these, you need to understand the idea of a class offering the outside world an "interface" to its functionality.

A particular class may contain many different methods, but many of them might be used only for internal processing within the class and it won't be necessary for any calling code to know about (or use) those methods.

A class should generally only have a limited number of methods available for the outside world to call. These methods make up the "public interface" of the class and should be marked with the public access modifier.

Any internal methods, used to assist in the function of the class but not necessary for calling code to use, should be marked with the private access modifier.

Protected is a special case, and is simple an extension of private which allows sub-classes to call methods on their parent classes. They are available only to the current class, and any sub-classes.


As a concrete example, consider a class which calculates the on hand inventory for an item.

We might have two possible uses for this class:

  1. Calculate the on hand qty in inventory (stocking) units
  2. Calculate the on hand qty in some specified unit using unit conversion

Therefore we offer two public methods:

  • public InventQty inventQtyOnHand()
  • public UnitQty unitQtyOnHand(UnitId _unitId)

The developer may decide to retrieve the on hand stock by summing the Qty field of the InventTrans table. It is not necessary for anybody calling the class to know that this is the logic being used. Therefore any methods used should be marked as private.

  • private void calcSumInventTransQty()

In the future, the class developer may realise that it's actually more efficient to use the InventSum table to calculate the stock on hand. In this case, they will probably delete the calcSumInventTransQty() method and replace it with one to retrieve the values from InventSum. As the original method was marked private, the class developer knows that nobody will have called it directly from outside his or her class, and therefore it is safe to modify or delete. If that private access modifier was not used initially, then there is a risk that any changes to the logic in the class may have an effect elsewhere in the application due to the method being called directly.

A class showing this example code can be downloaded here.


To summarise, correctly setting access modifiers on your classes has two advantages

  1. Other developers can see at a glance which methods they need to call to use the functionality of the class
  2. It is possible to change the internal workings of the class knowing that it won't break the calling code