Thursday 19 July 2012

INTERFACE


An interface is a reference type object with no implementation.  You can think of it as an abstract class with all the implementation stripped out and everything that is not public removed.  Abstract classes are classes that can not be instantiated.  No properties or methods are actually coded in an interface, they are only defined.  So the interface doesn't actually do anything but only has a signature for interaction with other classes or interfaces.  A good analogy for an interface is a pencil and a pencil sharpener.  When we talk of a pencil and a pencil sharpener we generally know what they do and how they interact. We don't have to know the inner workings of a particular sharpener or pencil, only how to use them through their interface.

 Example

public interface IPencil
{
void Write();
bool IsSharp { get; }
}
public interface IPencilSharpener
{
void Sharpen(IPencil pencil);
}


No comments:

Post a Comment