Interface in X++
what is interface?
answer:
-> interface is a specification for a set of public instance methods.
-> An interface defines and enforces similarities between unrelated classes without having to derive one class from the other.
-> All interfaces are public only even we don't explicitly add the public keyword in front of the interface keyword in the class declaration.
-> The methods of an interface are also public.
To create an interface, follow these steps:
- In Solution Explorer, right-click the project, and then select Add.
- In the Dynamics 365 Items select Code, select Interface and then enter a name for the interface.
- Select Add.
Interface Example:
public interface IDrivable
{
public int getSpeed() // public method
{
}
public void setSpeed(int newSpeed) //public method
{
}
}
***
-> When we add the
implements
keyword on a class declaration, the class must declare the methods that are specified by the interface. -> A class declaration can implement multiple interfaces.
-> List the interfaces after the single occurrence of the
implements
keyword and separate the interface names by using commas(,).-> All interface methods that a class implements must be explicitly declared as public by using the
public
keyword in the class.-> A class that implements an interface must also be declared as public.
-> An interface can extend another interface by using the
extends
keyword.-> However, an interface can't extend more than one interface.
class Automobile implements IDrivable
{
int m_speed;
public int getSpeed()
{
return m_speed;
}
public void setSpeed(int newSpeed)
{
m_speed = newSpeed;
}
}
Comments
Post a Comment