Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere

Interface

In Object Oriented Programming, an interface defines what the behavior an object will have, but it will not actually specify the behavior.It is basically a contract, that will guarantee that a object will have certain behaviors. Interface doesn't have implementation of methods.All the methods it has are abstract methods. Interfaces are just to provide a contract to other objects. Basic use of interfaces are to separates the implementation and defines the structure. This concept is very useful in cases where you need the implementation to be interchangeable. Apart from that an interface is very useful when the implementation changes frequently.

If we compare interface to our real world example then we can think electric power point as a interface it provide a point to other object like fan,cooler,T.V. and when we plug these object to that power point so that currernt can flow from electic boart to fan or cooler. power point itself doesn't do anything rather it's resposiblity of electric board or wire(class) to provide current to fan or cooler object.

If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract and the method definitions must be provided by the subclass that extends the abstract class. In addition to this an interfaces can inherit other interfaces.



Syntex-
Interface InterfaceName
{
Abstract methods;
}
ex-
Interface IPlug
{
void ProvideACCurrent();
}


what is the difference between a Class and an Interface?
1-A class can have abstract methods in it but interface always has only abstract methods in it.
2-A Class can be inherited from interface but a interface can not inherited from a class
3- A class can have private or public methods but interface can have only public method
4-A class emphasis the idea of encapsulation,while an interface emphasis the idea of abstraction (by suppressing the details of the implementation).