Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Creating a Subclass [AX 2012]

Updated: November 15, 2013

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 terminology, a superclass is often called a base class, and a subclass is often called a derived class.

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 fields.
    real x; 
    real y; 
    ;
 
    // Constructor to initialize fields x and y.
    new(real _x, real _y)
    { 
        x = _x;
        y = _y;
    }
}
 
class ThreePoint extends Point
{
    // Additional instance fields z. Fields x and y are inherited.
    real z; 
    ;

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

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

public final class Attribute 
 { 
     int objectField; 
 }

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.