Friday, August 3, 2018

Difference between Abstract and Interface

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.

In X++ an interface is a specification for a set of public instance methods. To create an interface, you begin by using the AOT to create a class. You edit the code of its classDeclaration node to replace the keyword class with the keyword interface. Then you add methods to the interface just as you would for a class, except no code is allowed inside the method bodies.

The purpose of interfaces is to define and enforce similarities between unrelated classes without having to artificially derive one class from the other.
Implements and extends
You can add the implements keyword on a class declaration, and this requires the class to declare the methods that are specified by the interface. A class declaration can implement multiple interfaces by listing them after the single occurrence of the implements keyword, with commas separating the interface names.

An interface can extend another interface by using the extends keyword. An interface cannot extend more than one interface.
Public
All interfaces are public regardless of whether you explicitly write the keyword public in front of the keyword interface in the classDeclaration. The methods on an interface are also public, and again the explicit inclusion of the keyword public is optional.
All interface methods that a class implements must be declared with the explicit keyword public in the class. Also, a class that implements an interface must also be declared with public.

Example :

// I say all motor vehicles should look like this:
interface MotorVehicle
{
    void run();

    int getFuel();
}

// My team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{

    int fuel;

    void run()
    {
        print("Wrroooooooom");
    }


    int getFuel()
    {
        return this.fuel;
    }
}
Implementing an interface consumes very little CPU, because it's not a class, just a bunch of names, and therefore there isn't any expensive look-up to do. It's great when it matters, such as in embedded devices.


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 a 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!".
For example:
// I say all motor vehicles should look like this:
abstract class MotorVehicle
{

    int fuel;

    // They ALL have fuel, so lets implement this for everybody.
    int getFuel()
    {
         return this.fuel;
    }

    // That can be very different, force them to provide their
    // own implementation.
    abstract void run();
}

// My teammate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
    void run()
    {
        print("Wrroooooooom");
    }
}

enter image description here

Source :https://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class

Source :https://msdn.microsoft.com/en-us/library/aa892319.aspx

1 comment:

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

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