Interface:
An interface is an empty shell. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern.
All interfaces are public regardless of whether you explicitly write the keyword public in front of the keyword interface in the class declaration. The methods on an interface are also public, and again the explicit inclusion of the keyword public is optional.
Abstract classes:
Abstract classes, unlike interfaces, are classes. They are more expensive to use because there is a look-up to do when you inherit from them.
Abstract classes look a lot like interfaces, but they have something more: You can define behavior for them. It's more about a person saying, "these classes should look like that, and they have that in common, so fill in the blanks!".
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.
Interface IDrivable
{
int getSpeed(){}
void setSpeed(int newSpeed){}
}
class Automobile implements IDrivable
{
int speed;
public int getSpeed()
{
return speed;
}
public void setSpeed(int newSpeed)
{
speed = newSpeed;
}
}
class UseAnAutomobile
{
void DriveAutomobile()
{
IDrivable drivable;
Automobile myAutomobile = new Automobile();
str temp;
myAutomobile = new Automobile();
if (myAutomobile is IDrivable)
{
drivable = myAutomobile;
drivable.setSpeed(42);
temp = int2str(drivable.getSpeed());
}
else
{
temp = "Instance is not an IDrivable.";
}
info(temp);
}
}
----------------------------------------Abstract class----------------------------------------
To understand the abstract class consider the following example
We have three classes
1. absClass (it’s an abstract class)
2. normal class (another class that will use the absClass methods)
3. extendAbsClass (this class will extend 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 class----------------------------------------
----------------------------------------Abstract method----------------------------------------
When we declare a method as abstract, this method should be overloaded in child class or we can say, this method should be declared/initiated in the child class then 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 the abstract method
}
2. class extendAbsClass extends absMethodClass
{
}
void absPrintName()
{
; // we have to initiate the 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.
----------------------------------------Abstract method----------------------------------------