Wednesday, August 26, 2015

Abstract Class and Abstract Method


Abstract Class:

Abstract classes are classes that contain one or more abstract methods.

An abstract method is a method that is declared, but contains no implementation.

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.


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.



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)

    1.abstract class absClass
     {
     }

    void printName()
   {
    ;    info("AbsClass... Deepak");
   }


    2. class extendAbsClass extends absClass
       {
        }

    3.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

1.      abstract class absMethodClass
{
}

abstract void absPrintName()
{
                        // we cannot declare any code in abstract method
}

2.      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");
}
3.      class childClass_1
{
}

void accessAbsClass()
{
    extendAbsClass  extendAbsClass;
    ;
    extendAbsClass  = new extendAbsClass();
    extendAbsClass.absPrintName();

}


Conclusion : 

  • Abstract classes can't be instantiated
  • they're explicitly intended to be extended. 
  • They also usually contain abstract methods, i.e. methods without any implementation that must be implemented by any non-abstract child. 
  • It allows you to define high-level concepts and provide some common functionality and leave details for concrete implementations. 


For example, RunBaseBatch class is abstract - it can't be used by itself because it doesn't define any action to run, but you can extend it and inherit a huge amount of functionality common to all batches.

1 comment:

  1. This guide on 'How to set fossil hybrid watch time' is a game-changer! I followed these instructions and got my watch set up in minutes. It's like having a brand new watch again. Thanks for the clarity and simplicity of the guide!

    ReplyDelete

How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// &l...