Interfaces
Although I danced around this subject in the tutorial, I never explicitly
tried to explain an interface. C# has a key work interface which allows you
to describe a contract without any implementation. An interface can contain
zero or more abstract methods. You cannot create an instance of an interface.
As I suggested in the tutorial, an interface is similar to a pure virtual
class in C++. It describes a set of method signatures, but does not provide
any implementation details.
A Twisted Analogy
If a class is a blueprint from which you can create zero or more objects
in memory, then you may ask what is an interface? Well, I have struggled
here
for a good analogy. You can think of an interface as one of many possible
abstract contracts between the builder of a custom building and the client.
If you are building tract homes, then you can simply use the stock blueprint,
modifying the usual properties such as color. If you are going to build
a custom rendition of a home using a blueprint, then the builder and client
will need come to verbal contracts about the building. These preliminary
verbal contracts do not describe the actual physical details of how the
building
will be constructed or embellished, but simply codifies an understanding
between the principals, the designer/builder and the client. The verbal
contract describes what the client wants, but not the gory details on how
the goal
will be implemented. One verbal agreement might be that building will be
be wired for cable TV. Another agreement might be that there will be a
home theater addition to the house. The designer/builder and client must
then
agree on an implementation of the abstract contracts before proceeding
with construction. Your construction project inherits from the blueprint
and the
designer/builder implements zero or more custom design decisions. Thus,
in the land of C#, a construction project (concrete class) can inherit
from
a single blueprint and implement zero or more contracts. More importantly,
every building in the land of C# is constructed with absolute precision!
Cool!
Sample Code: Pure Abstract Class vs. Interface
The general syntax for a pure abstract class and interface is:
abstract class MyAbstractClass
{
public abstract someType MyMethod(...);
...
}
interface MyInterface
{
someType MyMethod(...);
...
}
Not surprisingly, the syntax for inheriting from a class or implementing
an interface is the same:
class MyClass : MyAbstractClass, MyInterface ...
{
...
}
Here again is our pure abstract Drawable class from Chapter 4:
abstract class Drawable
{
public abstract String DrawYourself();
}
class Circle : Drawable
{
public override String DrawYourself()
{
return "Circle";
}
}
class Square : Drawable
{
public override String DrawYourself()
{
return "Square";
}
}
Since the base class Drawable does not contain any implementation details,
it can be rewritten as an interface. Here is a slightly different version
of the Drawable type implemented as an interface from Chapter 7:
// an interface version of Drawable
interface Drawable
{
void DrawYourself();
}
class Circle : Drawable
{
public void DrawYourself()
{
System.Console.WriteLine("Circle");
}
}
class Square : Drawable
{
public void DrawYourself()
{
System.Console.WriteLine("Square");
}
}
If you try to create an instance of the Drawable interface, the compiler will
complain:
Cannot create an instance of the abstract class or interface 'TestInterface.Drawable'
In general, if you are designing a contract without any implementation details,
then you should use an interface. Use an abstract class if you want to
include some implementation details. Remember, your class can only inherit
from one
class hierarchy, but can implement multiple interfaces. If you accept that
interfaces in C# are analogous to pure virtual classes in C++ then, in
essence, C# supports single inheritance of implementation and multiple
inheritance
of interface.