Building an abstract class is one way to define a type to achieve polymorphism, but another approach is to define and interface
. An interface definition uses the interface
keyword, and an interface is a type in .NET Framework. Just like the class, struct, and delegate keywords define types, so does interface.
An interface
is special because it can never contain any implementation details. When you define an interface you’re defining an API for an object, that is any object that implements an interface is guaranteed to have the member an interface describes.
public interface IWindow
{
string Title { get; set; }
void Draw();
void Open();
}
In the example above. Any object that implements IWindow must have a title
property that is both readable and writeable, it also must have a draw
method that takes no parameters and returns void, and it must have an Open
method, also a no parameters and returns void. In some ways an interface is like an abstract type, because in order to create a concrete class that implement this interface, it must implement those three members. But there is one huge difference between an abstract class and interface. When it defines a class, it can only inherit from a single base class, but it can implement as many interfaces as it likes.
Interface allows you to define the API your software needs without defining any of the implementation details, and because any class or struct inherit from any interface, interface are extremely flexible.
Interface Summary
An interface has the follow properties: