Monday, 23 December 2013

Abstract Modifier and Abstract Class in ax 2012

Abstract Modifier
An abstract class or method is the exact opposite of a final. The use of abstract classes is
a way of planning inheritance as it forces creating a subclass for using the class, as an
abstract class cannot be declared. This is often used in the standard package for super
classes to control that the super class is not declared by mistake. The class
SalesFormLetter which is used for creating documents such as sales confirmations and
sales invoices uses this practice. The class has an construct() method which should be
used, and to prevent the class being declared using new() SalesFormLetter is qualified
as abstract.
abstract CustAccount myAbstractMethod()
{
// The code in this method is never executed
#if.never
select firstonly custTable
where custTable;
#endif
}
Methods can be declared as abstract, but only if the class is abstract. An abstract method
must be overridden as an abstract method cannot have a code block. Abstract methods
contains only a parameter profile. You can however use the macro call #if.never to add
"code" to an abstract method. This will help clarify why this method must be overridden
in the subclass. You might wonder why you should not simply add comments in the
abstract method. The point is that code within the macro call is shown in color as with
any other code in the editor making it easier to differentiate comments and code. Note
that no validation is done on code written in the #if.never macro so anything could be
written. It is optional to qualify methods of an abstract class as abstract. For this reason
you will still be able to add variables in the ClassDeclaration of an abstract class..
The access modifiers protected can be use with abstract methods. An abstract method
cannot be static, as the static methods only exist in the super class


Abstract Class and Abstract Method in AXapta
Abstract Class:
When we declare a class as abstract, this class cannot initiate in X++ code. To use this class or its method we have to first extend this class than only we are able to use this class or its method. To understand the abstract class consider following example
We have three classes
     1.      absClass  (it’s an abstract class)
     2.      normalClass (an another class which will use the absClass methods)
     3.      extendAbsClass (this class will extends the absClass)
1st point.
abstract class absClass
{
}
void printName()
{
  ;
  info("AbsClass... Deepak");
}
2nd point.
class extendAbsClass extends absClass
{
}
3rd point.
class normalClass
{
}
void accessAbsClass()
{
    absClass        absClass;
    extendAbsClass  extendAbsClass;
    ;
    //absClass = new absClass();    // this declaration will throw error “Object could not be created because class absClass is abstract” so first we                //extend absClass into extendsAbsClass and further use extendsAbsClass to access absClass methods.
    extendAbsClass  = new extendAbsClass();
    extendAbsClass.printName();
}
Abstract Method:
When we declare a method as abstract , this method should be overload in child class or we can say , this method  should be declare/initiate in child class, than only we can use this class or its method.
Note:
a.      Abstract methods may only be declared in abstract classes.
b.      No code or declarations are allowed in abstract methods.
We have three classes
i. absMethodClass
ii.extendAbsClass
iii.NormalClass
1st point.
abstract class absMethodClass
{
}
abstract void absPrintName()
{
   // we cannot declare any code in abstract method
}
2nd point.     
class extendAbsClass extends absMethodClass
{
}
void absPrintName()
{
    ; // we have to initiate abstract method here as this class extends the abstract class.
    info("abstract method declaration in derived class");
}
3rd point.

class childClass_1
{
}
void accessAbsClass()
{
    extendAbsClass  extendAbsClass;
    ;
    extendAbsClass  = new extendAbsClass();
    extendAbsClass.absPrintName();
}

No comments:

Post a Comment