Inheritance in D365 F&O - X++ Code

Rumman Ansari   Software Engineer   2023-10-19   1715 Share
☰ Table of Contents

Table of Content:


Introduction to Inheritance in X++

  • Inheritance in X++ is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
  • The idea behind inheritance in X++ is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
  • Inheritance concepts are used to create parent-child relationships between classes.
  • Inheritance allows a subclass to extend a super class.
  • The subclass inherits all the attributes and methods of the super class. Subclasses can also override the behavior of or add more functionality to methods that are inherited from the super class.

Inheritance and abstract classes are two important programming constructs that are found in object-oriented programming languages. These concepts are used to create parent-child relationships between classes.

Inheritance allows a subclass to extend a super class. The subclass inherits all the attributes and methods of the super class. Subclasses can also override the behavior of or add more functionality to methods that are inherited from the super class.


Types of Inheritance: in X++ Programming Language - D365 Finance and Operation

In the X++ programming language within the context of D365 Finance and Operations, you can implement different types of inheritance to create class relationships. Here are the main types of inheritance:

  1. Single Inheritance: In single inheritance, a class inherits from only one parent class. This is the most basic form of inheritance, where a child class inherits attributes and methods from a single parent class.

  2. Multilevel Inheritance: Multilevel inheritance involves a chain of classes, where each class inherits from the class above it in the hierarchy. For example, Class A can be the parent of Class B, and Class B can be the parent of Class C. Class C inherits from both Class B and Class A.

  3. Multiple Inheritance (Interface-based): While X++ does not directly support multiple inheritance (where a class inherits from multiple parent classes), it supports interface-based inheritance. Interfaces define a set of methods that implementing classes must provide. A class can implement multiple interfaces, effectively allowing it to inherit behavior from multiple sources.

  4. Hybrid Inheritance: Hybrid inheritance combines multiple inheritance with other types of inheritance. For example, a class might inherit from a single parent class and implement one or more interfaces. This approach enables a combination of code reuse and interface-based behavior.

It's important to note that X++ uses the concept of "extends" for class inheritance and "implements" for interface-based inheritance. While X++ doesn't support traditional multiple inheritance, interface-based inheritance provides a mechanism to achieve similar functionality while promoting code modularity and avoiding ambiguities that can arise in multiple inheritance scenarios.

When implementing inheritance in X++ for D365 Finance and Operations, consider the design goals of your application and choose the most suitable type of inheritance to achieve them.



Inheritance: Single Inheritance

Single Inheritance: In single inheritance, a class inherits from only one parent class. This is the most basic form of inheritance, where a child class inherits attributes and methods from a single parent class.

To better understand the concept of inheritance in object-oriented programming, lets review a real-world example. In this example, Vehicle is the parent class. There are many different types of vehicles, for instance: cars, buses, and trucks. In this scenario, Vehicle is the parent class. Cars, buses, and trucks are derived classes that inherit from Vehicle. The Vehicle class has characteristics that are shared across all the vehicle types (speed, color, and gears for example). The characteristics for each of the vehicles types may be different. For instance, a truck might have all-wheel drive, but a car does not.

Inheritance in D365 F&O

Example

The following example demonstrates how a class uses inheritance. The Car class extends the Vehicle class to get the height and width but also adds the number of passengers variable.


class VehicleClass
{
    // Instance fields.
   real height;
   real width;
   
   // Constructor to initialize fields height and width.
  void new(real _height, real _width)
   {
      height = _height;
      width = _width;
   }
}


class Car extends VehicleClass
	{
	  // Additional instance field numberOfPassengers. Fields height and width are inherited.
 	  int numberOfPassengers;

	 // Constructor is overridden to initialize z.
	 void new(real _height, real _width, int _numberOfPassengers)
 	 {
	   // Initialize the fields.
	   super(_height, _width);
 	   numberOfPassengers = _numberOfPassengers;
 	 }
	}

Run your code using Main Method. You can write it in another class or you can write it in you child class ( Car) also.


public static void main(Args arg)    {        
    CarClass obj = new CarClass(12.0, 12.0, 3);        
    obj.run();        info(strFmt("Height %1", obj.height));        
    info(strFmt("Width %1", obj.width));        
    info(strFmt("number Of Passengers %1", obj.numberOfPassengers));   
     } 

When Microsoft Dynamics 365 uses inheritance?

Microsoft Dynamics 365 is a complex software suite that consists of many different components, including various classes, objects, and frameworks, all of which are written in X++ language. Inheritance is used extensively in Dynamics 365 to achieve modularization and code reuse.

For example, the base data model classes in Dynamics 365, such as the CustTable and VendTable classes, are designed to be extended by developers to add custom fields and functionality. These classes are organized in a hierarchy based on inheritance, with more specialized classes inheriting from more general classes. This allows developers to leverage existing code and functionality and build upon it to create customized solutions.

In addition, Dynamics 365 also provides various frameworks and patterns that utilize inheritance, such as the Chain of Command framework, which allows developers to extend existing code without modifying it directly, and the Model-View-Controller (MVC) pattern, which separates the user interface (view) from the data model (model) and the business logic (controller) for better maintainability and scalability.

In Microsoft Dynamics 365, inheritance is used in the development of customized classes and tables. Custom classes can inherit properties and methods from base classes, and custom tables can inherit fields and relations from base tables.

Here is an example of inheritance in X++ code:


class MyTable extends CustTable
{
    // Add custom fields and methods here
}


In this example, the MyTable class is defined as an extension of the CustTable class, which is a base table provided by Microsoft Dynamics 365. By extending CustTable, MyTable inherits all of its fields, relations, and table methods. MyTable can also define its own fields and methods, which will be specific to the custom table.

Another example of inheritance in X++ is with classes:


class MyCustomClass extends SysOperationServiceController
{
    // Add custom methods here
}

In this example, the MyCustomClass class is defined as an extension of the SysOperationServiceController class, which is a base class provided by Microsoft Dynamics 365. By extending SysOperationServiceController, MyCustomClass inherits all of its methods, properties, and events. MyCustomClass can also define its own methods and properties, which will be specific to the custom class.


Question 1: What is inheritance in object-oriented programming?

Answer: A) A mechanism for code reuse and creating a new class from an existing class.

Explanation: Inheritance allows you to create a new class (subclass or derived class) by deriving properties and behaviors from an existing class (base class or superclass), enabling code reuse and extension.

Question 2: Inheritance allows a subclass to _______.

Answer: C) Inherit both the public and protected members of the superclass.

Explanation: Inheritance allows a subclass to inherit both the public and protected members (methods and attributes) of the superclass, although it cannot access the private members directly.

Question 3: Inheritance supports which of the following principles of object-oriented programming?

Answer: D) All of the above

Explanation: Inheritance is a fundamental concept in object-oriented programming and supports multiple principles, including encapsulation, polymorphism, and abstraction.

Question 4: What is a base class or superclass in the context of inheritance?

Answer: C) A class that provides a blueprint for other classes.

Explanation: A base class or superclass is a class that provides a blueprint for other classes (subclasses or derived classes) to inherit attributes and methods.

Question 5: In a class hierarchy, the class that inherits from another class is known as the _______.

Answer: C) Derived class

Explanation: In a class hierarchy, the class that inherits from another class (superclass) is known as the derived class or subclass.