This topic has not yet been rated - Rate this topic

Creating a Subclass [AX 2012]

Updated: September 30, 2011

Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

Subclasses are classes that extend (inherit from) other classes. In X++, a new class can only extend one other class; multiple inheritance is not supported. If you extend a class, it inherits all the methods and variables in the parent class (the superclass).

Subclasses enable you to reuse existing code for a more specific purpose, saving time on design, development, and testing. To customize the behavior of the superclass, override the methods in your subclass.

For more information about overriding a method, see Overriding a Method.

The following example creates a class called Point and extends it to create a new class called ThreePoint.

class Point
{
    // Instance variables
    real x; 
    real y; 
    ;
 
    // Constructor to initialize x and y
    static new(real _x, real _y)
    { 
        x = _x;
        y = _y;
    }
}
 
class ThreePoint extends Point
{
    // Additional instance variable; x and y are inherited
    real z; 
    ;

    // Constructor is overridden to initialize z
    static new(real _x, real _y, real _z)
    {
        // Initialize the variables
        super(_x, _y); 
        z = _z;
    }
}

You can prevent classes from being extended by using the final modifier:

public final class Attribute 
 { 
     int objectVariable; 
 }

Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.