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 :
Finance | Project Operations, Human Resources, ...
Answered

[Overriding a method] subclass method is not called from superclass

(0) ShareShare
ReportReport
Posted on by 678

Hi, 

I have created a subclass [own model to override a method 'validate' of a class 'InventABCUpdate' [Application Suite model. Then I gave the same name and parameters as the method in the superclass. 

However, this method in subclass is not called whilst debugging the superclass, but instead, the original method in superclass 'InventABCUpdate' is called.

I think the method overriding the one in superclass is somehow ignored. 

class InventABCUpdateExtendedValidate extends InventABCUpdate
{
    public boolean validate(Object _calledFrom = null)
    // this method signaure is the same as the one in superclass
    {
        boolean ok = true;

        if (model == ABCModel::Link)
        {
            if (interest <= 0)
            {
                ok = checkFailed("@SYS8377") && ok;
            }
            if (! toDate)
            {
                ok = checkFailed("@SYS24515") && ok;
        
        // there are other if clauses but not written here

why is it not called? 

Thank you. 

I have the same question (0)
  • Verified answer
    Pete Alberts Profile Picture
    3,542 on at

    That means that the object that was instantiated is of type InventABCUpdate and not InventABCUpdateExtendValidate.

    I think you actually wanted to create an extension/aggregation (AX version of 'extension'). But then you created a child class (classic coding 'extension' concept).

    Are you trying to add logic to the original class or trying to create a new version which inherits from the original?

  • Verified answer
    Pete Alberts Profile Picture
    3,542 on at

    What I think you actually wanted to do is this:

    [ExtensionOf(classStr(InventABCUpdate))]
    final class InventABCUpdateXYZ_Extension
    {
        boolean validate(Object _calledFrom)
        {
            boolean validate;
            
            //your logic
            
            validate = next validate(_calledFrom);
            
            return validate;
        }
    }

  • ShawnDEV Profile Picture
    678 on at

    You are right. I was a bit confused between two concepts, 1) X inheritance and 2) Class extension using CoC.  

    What I want to achieve is to augment an existing method in InventABCUpdate which is a core class.

    According to some materials found online, we can override the method by creating a subclass and giving a method the same name as the one in super class with the same parameters. But as you mentioned it was not the way to use. 

    I will try to use CoC.

    Just one more question:

    There are a series of IF clauses in the original method 'validate()' under the class inventABCUpdate. These IF clauses are in parallel in terms of application.

    If I want to add additional IF clause to this method before returning final value. How should I write it in a code in CoC?  

     public boolean validate(Object _calledFrom = null)
        {
            boolean ok = true;
    
            if (fromDate && toDate && fromDate > toDate)
            {
                ok = checkFailed("@SYS91020");
            }
    		
    		// add additional if clause before returing the final value 
    
            return ok;
        }

  • Suggested answer
    Pete Alberts Profile Picture
    3,542 on at

    Glad I could help! I think I know what material you are referencing regarding the "same name" thing. That is an old method to achieve what we now do with CoC.

    I don't fully understand your new question. But I think I can help by making this comment: I handle the "next" call the same as I handle the "super" call. Only difference is "next" is mandatory and has some other restrictions. But the compiler will let you know about those.

    Here is what I mean in code:

    [ExtensionOf(tableStr(CustTable))]
    final class CustTableXYZ_Extension
    {
        boolean validateWrite()
        {
            boolean valdiateWrite;
            
            validateWrite = next validateWrite();
            
            if (someCondition)
            {
                validateWrite = checkFailed('asdf');
            }
            
            return validateWrite;
        }
    }
    
    //Above is the same logic as a normal OOP method override:
    class XYZMyTable extends Common
    {
        boolean validateWrite()
        {
            boolean validateWrite;
            
            validateWrite = super();
            
            if (someCondition)
            {
                validateWrite = checkFailed('asdf');
            }
            
            return validateWrite;
        }
    }
    

  • Pete Alberts Profile Picture
    3,542 on at

    Maybe my way of coding confuses you. The following lines do the same thing in the right conditions:

    ok = checkFailed('asdf');

    ok = chechFailed('asdf') && ok;

  • Pete Alberts Profile Picture
    3,542 on at

    Taking your original example. I'd write it like this:

    public boolean validate(Object _calledFrom = null)
    // this method signaure is the same as the one in superclass
    {
        boolean ok = true;
    
        if (model == ABCModel::Link)
        {
            if (interest <= 0)
            {
            //the "&& ok" is not necessary because
            //1) false && false == false
            //2) false && true == false
            //meaning whatever value "ok" was, it does not matter, the new value will still be false
                ok = checkFailed("@SYS8377");// && ok;
            }
            if (! toDate)
            {
                ok = checkFailed("@SYS24515");// && ok;
        
        // there are other if clauses but not written here

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 456 Super User 2025 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 429 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans