No items found.

OOP101 in AX 2012 part 2: Inheritance and abstract classes

The great feature of inheritance is that it brings in a possibility to create a hierarchical structures. Inheritance describe “is-a” relationship and it is used whenever there is a need to have a more specialized class that has all the properties of base class and more.  For example let’s consider a situation from OOP101 in AX 2012 part 1 where we have a Car. This time let’s look at the Car as a part of the bigger group called Vehicles, we can model this relationship with an inheritance pattern like below.

in Vehicle class is denoted using italics this means that it is abstract which implies that the Vehicle class has to be an abstract class, also we can see that the method printInfo has been overridden in derived class. Vehicle is an abstract because we should not be able to instantiate it on its own, ultimately what is a vehicle without a context like a particular car … exactly … it is an abstract concept. One of the most important advantages of inheritance over interfaces is that we can provide a default functionality. But what if we know that all the derived classes should have a functionality (like operate) but don’t have enough details in the context of the base (abstract) class?  In such a scenario the solution is to create an abstract method which is basically a contract that obliges us to provide functionality later in the derived classes.  In X++ we can provide only one base class; it does not support multiple inheritance like for example C++, but that is actually a good thing because it protects us developers from implementing ambiguous scenarios like the diamond problem. In such a scenarios where we wish that we could use multiple inheritance we can use interfaces instead.

X++ implementation of Vehicle/Car scenario could look as follows:

abstract class Vehicle

{

   str owner;

   int age;

   void printInfo()

   {

    /* default functionality */

   }

   abstract void operate()

   {

   }

}

class Car extends Vehicle

{

   str model;

   void printInfo()

   {

    /* overriding default functionality */

   }

   abstract void operate()

   {

/* implementation */

   }

}

It is also worth noting that when we are overriding functionality in derived classes we can call the default functionality from the base class by using super keyword. In part three of this series I will go through the major benefit that implementing inheritance and interfaces gives, namely polymorphism.

See more

AGA pomaga – poznaj inteligentnego asystenta do przetwarzania zapytań ofertowych: ANEGIS Generative Assistant

Read article
Text Link

Jak z pomocą narzędzi Power Platform, Microsoft Fabric i AI zrealizować plan na cyfrową transformację? ANEGIS Power Center

Read article
Text Link

Obowiązkowy KSeF startuje za 4 miesiące! Umów się na bezpłatne konsultacje i przygotuj swój system na zmiany

Read article
Text Link
Back to all

Let's talk about your project

Let us know what you need: implementation, migration, system development, or a question you’re looking to have answered. We’ll get back to you with a concrete proposal for the next step - not a sales pitch.

Thank you for your message!
Oops! Something went wrong while submitting the form.
Microsoft AI Builder
Microsoft Copilot Studio
No items found.
No items found.
No items found.