web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Inheritance/Method Access Control

(0) ShareShare
ReportReport
Posted on by

Hello all,

I have an easy question around method access control I wanted to ask to make sure I'm understanding the concept.

Here would be the scenario.

A Parent Class is created and there is a Parent method in that class.

several child classes will inherit, using extends, from the parent class to use the Parent method.

To ensure the Parent method is available to any Child Class I would set the method modifier in the Parent Method to Protected.

This will allow the Child Class to see the Parent Method.

Alternatively if I set the Parent method to the default Private modifier, the Parent method would not be seen from the child class.

Finally if I set the Parent Method to the Public modifier the Parent method becomes available to any other class which ultimately can lead to bad coding practices and makes things more difficult for debugging.

Is that correct?

Please let me know if it isn't or feel free to add additional comments.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    Hi

    Please check the documentation on MSDN: msdn.microsoft.com/.../aa676482.aspx

    In AX you typically use public and private keywords for customizations, unless you are writing new framework classes that you intentionally want to be extensible by other partners/customers and using those protected methods within the framework itself.

  • Community Member Profile Picture
    on at

    Vilmos, Thanks for your reply.

    From a best practice perspective you would never recommend using the Protected access modifier?

  • Verified answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    Protected methods are widely used. You use them every time when you want some methods to be used by child classes, but not by classes outside the inheritance hierarchy.

    Object-oriented development isn't limited to frameworks extended by other companies.

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    Take a look at the standard FormLetter class, which is a framework.

    There, they are declaring a method like this:

    abstract protected PrintMgmtNodeType printMgmtNodeType()
    {
    }

    There is only an internal call for that in the class in checkClientOutputPrintManagement, thus that is why it is not publicly exposed.

    On the class if you see Right click > Add-ins > Type hierarchy browser, you would see how it is being extended by ProjFormLetter class, which does have the method overriden:

    protected PrintMgmtNodeType printMgmtNodeType()
    {
        return PrintMgmtNodeType::Project;
    }

    If you have no need to explicitly define method abstracts and extend the method by a child class, you are perfectly fine with private/public declarations. Otherwise you need to use the protected keyword.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans