Difference between Abstract class and Implemetation

Hi All,

Can any one explain and give example of abstract class and implementation.

when to use abstract class and implementation.

Thanks’

nihar

There are many sources about object-oriented development, it would be better if you check them - don’t expect to became a developer just by asking in forums.

I suppose you know non-abstract classes. 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. That 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.

Abstract Class and Abstract Method in AXapta

Abstract Class and Abstract Method****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)

  4. abstract class absClass
    {
    }

void printName()
{
; info(“AbsClass… Deepak”);
}

  1. class extendAbsClass extends absClass
    {
    }

  2. 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
}

  1. 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();

}

An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.

An interface is an empty shell, just only the signatures of the methods. The methods do not contain anything. The interface can’t do anything. It’s just a pattern. An Abstract class is a class which will contains both definition and implementation in it.

Abstract classes can have consts, members, method stubs and defined methods, whereas interfaces can only have consts and methods. More about…Abstract Class and Interface

Walsh

It’s true that classes can implement multiple interfaces, nevertheless interfaces in X++ don’t support t multiple inheritance. As classes, they can inherit from a single parent interface only.