Inheritance and Method Overriding in X++ Code - Best Practices

Rumman Ansari   Software Engineer   2023-08-20   737 Share
☰ Table of Contents

Table of Content:


Ineritance - Method Overridden - X++ Code

Parent Class


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;    
        }      

        void run()
        { 
          info("Running");    
        }
}

Child Class


class CarClass extends VehicleClass{

        // Additional instance field numberOfPassengers. Fields height and width are inherited.   
 
        int numberOfPassengers;     

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

        // Method Override
        void run(){            
                super();            
                info("running from child class - car");  
        }      

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));   
     } 
 }

Output


number Of Passengers 3
Width 12.00
Height 12.00
running from child class - car
Running