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)

Can abstract only be used as a method if the class is declared abstract also?

(0) ShareShare
ReportReport
Posted on by 7,437

Hi,

Small syntax question about X++ development. I have now declared next and it works:

abstract class A
{
public abstract int function1(){}
}

The extended subclass which extends class A is:

class B extends class A
{
public int function1(){ return 10;}
}

From C# I first coded:

class A
{
public abstract int function1(){}
}

However, it seems that the next error is thrown: "Abstract methods may only be declared in abstract classes.". I then tried:

class A
{
public int function1(){ return -1}
}

However, now the function1() is not implemented in class B although it extends class A. -1 is returned instead of 10. I do not see any modifier like override or virtual as used in C#. Do I miss something or is this just how the compiler of X++ works? I know I should RTFM.

J.

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    AX forces you to declare a class as abstract, if it contains abstract methods. Methods are virtual by default and there is nothing like "override/new" specifier for methods in X++.

    About "now the function1() is not implemented in class B although it extends class A". Are you sure that your instance has right type (B)? It is not shown in your snippet.

  • user5555 Profile Picture
    7,437 on at

    Hi Martin,

    Thank you as ever!

    I used the abstract and it works. I had the feeling override/new are not done by using the same method. But might also me fighting with the compiler at times. It works and I am happy we can import a ton of items without entering them manually :-).

    J.

  • Community Member Profile Picture
    on at

    Hi J,

    An abstract method must be part of an abstract class. An abstract class may have both abstract and instance methods. When you extend an abstract class, you must implement all abstract methods in the subclass. You can override an instance method by right-clicking on the the subclass, and selecting "Override method", then the method you want to override. AX will automatically call "super()" in the overriden method, but you can choose to change this as you see fit.

    Hope this helps. --Tom

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