Abstract Class in D365 F&O - X++ Code
Table of Content:
Abstract
Abstract classes are identified by the keyword abstract
. They cannot be instantiated but require a subclass that is inherited from the abstract class. Abstract classes are used to implement a concept that the subclass will then complete. Abstract methods can also be declared in abstract classes. Abstract methods do not allow code or declarations in the method. A subclass must extend the abstract class to use the abstract method.
Example
The following is an example of an abstract class. The Car class extends the abstract class Vehicle. This allows the Car class to override the printInfo()
method and it allows you to add functionality to the operate()
method that was not included in the abstract class.
Parent Class Vehicle Which is abstract
abstract class VehicleClass{ str owner = "Rumman"; int age = 25; // below method is not abstract void printInfo() { Info(strfmt("%1, %2",owner, age)); } // This is a abstract method abstract void operate() {} }
Child Class Car which is extending the Parent Class
class CarClass extends VehicleClass{ str model = "Model Awesome"; // this printInfo method is overrding the parent method void printInfo() { // overriding default functionality Info(strfmt("%1, %2, %3",owner, age, model)); } // Here is the implementation of operate method which is present in the abstract class void operate() { Info('running'); } public static void main(Args arg) { CarClass obj = new CarClass(); obj.printInfo(); // call non abstract method obj.operate(); // class abstract method } }
Output
running Rumman, 25, Model Awesome