Microsoft Dynamics AX 2012 R3 Development-2 Locus IT Services

Microsoft Dynamics AX 2012 R3 Development-2

Locus IT ServicesMicrosoft Dynamics AX 2012 R3 Development-2

Lesson 3 – Method Access Control

Method Access Control:

Method Access Control In X++ explains, you use the accessor keywords public, protected, and private to control whether the methods in other classes can call the methods on your class. The accessor keywords on methods also interact with the rules for class inheritance. The following table describes the accessor keywords you use with methods.

public Method access control that are declared as public can be called from anywhere the class is accessible. In addition, a public method can be overridden by a subclass, unless the method is declared as final.
protected Methods that are declared as protected can be called only from the following:

  • From methods in the class.
  • From methods in a subclass of the class that contains the protected method.Methods that are protected can be overridden in subclasses.
private Methods that are declared as private can be called only from methods in the class where the private method is declared. No private method can be overridden in a subclass.

When you create a new method, the default accessor keyword that appears in the code editor is private. This is the most conservative default for maximum security.

Static and Instance Methods

The accessor keywords on methods never restrict call between two methods that are in the same class. This is true regardless of which of the two methods are static or non-static.

In a static method, calls to the new constructor method are valid even if the new constructor method is decorated with the private modifier. The syntax for these calls requires the use of the new keyword of X++. The code in a static method must construct an instance object of its own class before the code can call any instance methods on the class.

Increase Access When Overriding

When a method is overridden in a subclass, the overriding method must be at least as accessible as the overridden method. For example, the following X++ compiler rules apply to overriding a protected method in a subclass:

  • A public method in a superclass can be overridden only by a public method in the subclass.
  • In a subclass, a public method or a protected method can override a protected method of the superclass.
  • In a subclass, a private method cannot override a protected method of the superclass.
Locus IT Project Management Office
What’s it?