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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / Override method of an ...
Finance forum

Override method of an extension class in a class extension

(0) ShareShare
ReportReport
Posted on by

Hi everybody,

please consider this situation: you create an extension class MyClass_Extension of a class Class, where you add a new method and you augment the functionality of an existing method of the class Class using chain of command to call the new method. Moreover you extend Class in MyClassSon, overriding the new method introduced with MyClass_Extension.

If you try to call the new method from an instance of MyClassSon, D365 executes the code of MyClass_Extension. Is there any way to override it in MyClassSon?

Here the minimal example to reproduce it:

class AClass
{
	public void run()
	{
		info("AClass");
	}
}

//extension class
[ExtensionOf(classStr(AClass))]
final class AClass_Extension
{
	public void run()
	{		
		next run();
		
		this.myRun();
	}
	
	public void myRun()
	{
		info("AClass_Extension");
	}
}

// class extension
class AClassExtension extends AClass
{
	public void myRun()
	{
		info("AClassExtension");
	}
}

class RunnableTest
{
    static void main (Args _args)
    {
        AClassExtension a = new AClassExtension();
 
        a.run();
	// I obtain the output:
	// - AClass
	// - AClass_Extension
	// where I would like to obtain
	// - AClass
	// - AClassExtension
    }
}


My doubt is that since MyClass_Extension must be declared final, its methods cannot be overridden, but I can't find this information in
MS documentation. Can you please confirm this?

Thank you very much and regards,
Nicola

I have the same question (0)
  • Verified answer
    Sukrut Parab Profile Picture
    71,735 Moderator on at

    Hi Nicola ,

    You are getting that because, when you call next(), it checks for other extensions in the queue and runs them in random order, lastly running the base code. Try using is and As keyword in your scenario because  AClassExtension  is inherited class

    msdn.microsoft.com/.../gg843452.aspx

  • Verified answer
    Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    AClassExtension can overridde methods of its parent class, which is AClass. AClass_Extension is a separate class, it isn't and can't be the parent of AClassExtension. Therefore you two myRun() methods are two indepent things. Also realize that extensions are typically in a different package than the augmented classes and may not be installed at all, therefore you can't expect that AClassExtension will override a method that is isn't aware of at compile time and which doesn't have to exist at all.

    Overriding is about inheritance, not extensions.

    By the way the naming (AClass_Extension vs AClassExtension) is very confusing.

  • Nicola Zago Profile Picture
    on at

    Hi Sukrut, hi Martin,

    thank you for the clarification, I thought I could override any public method of the augmented class with a derived class (in my case they are both in the same package). My doubt was due to the fact that debugging the run method of AClass_Extension the @this element had type "Dynamics.AX.Application.AClass {Dynamics.AX.Application.AClassExtension}", so the system was aware of the correct type of the class, but D365 used the method myRun from AClass_Extension in place of AClassExtension.

    Now I understand that the runnable class calls the run method of AClass (which just calls info("AClass")), and than the chain of command executes the run method of AClass_Extension which is not aware of AClassExtension, so it calls the myRun method of AClass_Extension.

    Sorry for the poor notation, I thought I used also in the code example the notation MyClass_Extension and MyClassSon.

    So, like suggested by Sukrut, a possible strategy is to check for the actual type of the class in the "myRun" method declared in the AClass_Extension removing it from AClassExtension (at least in my case where both classes are in the same package)?

    Something like:

    public void myRun()
    {
    	if (this is AClassExtension)
    	{
    		info("AClassExtension");
    	}
    	else
    	{
    		info("AClass_Extension");
    	}
    }

    Thank you,

    Nicola

  • Dick de Jong Profile Picture
    9 on at

    Martin, i agree with what you say is how it works, however the following link might be confusing then, or? 

    https://docs.microsoft.com/en-us/dynamics365/unified-operations/dev-itpro/extensibility/class-extensions

    From the above link the following part is confusing i feel. Where in the sub class you do have access to the public methods of the effective class, you cannot OVERRIDE them, or?  If in a different package, i agree, if in same package that should be possible i feel. This gives you provision for at least the concept of an abstract method in a base class which is implemented in a sub class ... (=Design pattern "Template pattern")

    Extension class inheritance

    Any class that inherits from an augmented class also inherits the effective class. In other words, the classes that inherit from a class that has extensions inherit the methods that are defined in the extension classes.

    E.g. 

    AClass_Extension()

     public void helloWorld()

     { 

          if (this.canSayHelloWorld())

          {

                 info("hello world in extension class");

          }

          public boolean canSayHelloWorld()

          { return true; }

     }

    AClass_Son (extends AClass)

    {

         public boolean canSayHelloWorld()

         { return false;  }

    }

  • Rafal Krypel Profile Picture
    85 on at

    Hi,

    I have similar problem. Two standard classes: ParentClass and ChildClass. ChildClass inherits from ParentClass. Then I've created ParentClass_Extension class and added method newMethod. Then, I've created ChildClass_Extension class, and overloaded newMethod.

    When I run method ChildClass.newMethod, the code runs newMethod from ParentClass, instead od running newMethod from ChildClass. Is that expected behavior ?

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    The question is now you call the method. If the declared type of the variable is ParentClass, the expected method to be called is ParentClass_Extension.newMethod(). If the variable is declared as ChildClass, ChildClass_Extension.newMethod() should be called.

    Note that you neither overloaded or overrode newMethod() in ChildClass_Extension. You created a new independent method there.

    At least, that's my understanding.

  • Rafal Krypel Profile Picture
    85 on at

    I call the code via:

    ParentClass obj = new ChildClass();

    obj.newMethod();

    The newMethod() is added via extension to ParentClass (ParentClass_Extension.newMethod() and to child class ChildClass_Extension.newMethod())

    What I thought is that ChildClass_Extension.newMethod() should be called, but it is not. The ParentClass_Extension.newMethod() is beeing called.

    And i did create newMethod in ChildClass_Extension, it is not a new independent method.

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    As I explained, your expectation is wrong. ParentClass_Extension is expected to be called in this case. There is no inheritance on class extensions - what method will be called is determined at compile time based on the type of the variable, not polymorphically at runtime based on the actual value.

  • Rafal Krypel Profile Picture
    85 on at

    Just to be sure that You understand my issue. What You want to say is that adding via extension new method to standard ParentClass and overloading it (adding the same newMethod to ChildClass which inherits from ParentClass) results in calling the method from ParenClass, even that it is called via: "new ChildClass().newMethod()" ? Maybye I'm missing something - but it does not make any sense to me..

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Overloading is creating a method with the same name but different parameters (which isn't supported in X++). You mean that you overrode the method, but you didn't, because there is no inheritance relation between ParentClass_Extension and ChildClass_Extension. You just created a method with the same name, but it doesn't override the method in ParentClass_Extension, because that's not a parent of ChildClass_Extension.

    You must accept that adding methods to an extension isn't the same thing as adding methods to the augmented class.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans