Inheritance in D365 F&O - X++

 

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. v

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

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 Vehicle
{
    // Instance fields.
   real height;
   real width;
   
   // Constructor to initialize fields height and width.
   new(real _height, real _width)
   {
      height = _height;
      width = _width;
   }
}


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

	 // Constructor is overridden to initialize z.
	 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 CustTableMyTable 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 SysOperationServiceControllerMyCustomClass 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.


Comments