Interfaces

As you have seen in the previous post, Abstract Base Classes can have both abstract as well as non-abstract methods. But if you need to define a class, which just contains abstract methods and nothing more, in other words a “pure abstract base class” you would create an Interface.

An interface is very much similar to a pure abstract base class. It can contain only abstract methods and no method implementations. An instance of an Interface can never be created. An interface instead is used to indicate that a class that implements the particular interface must implement the members listed by the interface.

Earlier, we had said that classes are like moulds that define the methods and data members an object should have. Similarly, an interface can be considered as being a mould for a class. It indicated what a class must provide.

Let us take a look at some code depicting Interfaces. Consider Example 6,

Example 6:

Interfaces Example 6

Example 6 depicts an interface. Interfaces are defined in the same way as classes except that the interface keyword needs to be specified after the access modifier, followed by an interface name. Interface names usually begin with the letter I depicting it to be an interface.

The method definitions go in between the open and close curly braces. In Example 6, we have defined two operations (remember in the last post we had pointed out that ‘operations’ is the name given to methods which do not have an implementation). The two operations are DeleteImage(), which returns an integer and DisplayImage(), which neither takes in any parameters nor returns any value. Also interface members do not have any access modifier, it is the class, which inherits them, that sets their visibility.

Interfaces can also have data members. However, an interface cannot have fields or only data members. It can only have properties. We shall discuss properties, their types and how to create them, in a later post.

Example 7 implements the interface shown in Example 6.

(Note the terminology, when a class makes use of an interface it (the class) is said to implement the interface.)

Example 7:

Interfaces Example 7

If we merge the code in Example 6 and Example 7 and compile it, it would give us the following output:

DisplayImage Implementation!
DeleteImage Implementation!

In Example 7 we create a new class, MyImages, which implements the interface IPict.

  • Note the syntax for implementing the interface. As in case of implementing inheritance, here too, the colon (:) operator is used.
  • Within the curly braces we have defined the implementations for the methods in the interface. An interesting point to note here is that unlike in the case of abstract base classes, we are not overriding the methods but just implementing them, hence, the keyword override need not be specified at all.
  • The manner in which the class is instantiated in the Main() method and the manner in which its methods are called remain unchanged.

A class can implement an interface as well as inherit from another class. Consider another class called BaseIO with the implementation as shown in Example 8.

Example 8:

Interfaces Example 8

Now, suppose we want to inherit the MyImages class from this class. We can always do so. The code would look as shown in Example 9.

Example 9:

Interfaces Example 9

The output of Example 9 will be as shown below:

DisplayImage Implementation!
DeleteImage Implementation!
This is the Open method of BaseIO

(If you have to get the above result you need to add the code in Example 8 and Example 6 into Example 9.)

Note that if you want to inherit from a class as well as implement an interface you use a comma (,) between the class name you wish to inherit from and the interface you wish to implement. The rest remains just the same.

1.   Multiple Interface Implementation

C# does not allow multiple class inheritance. However, it allows multiple interface implementations. A class can implement more than one interface. Consider Example 10 as an extension of the previous examples.

Suppose we have another interface called IPictManip, which consists of only one method to apply alpha blending to our images. The code for this will be as shown in Example 10,

Example 10:

Interfaces Example 10

If we wanted to use the functionality of the above-mentioned interface into the class MyImages (meaning implementing two interfaces, IPict and IPictManip) we will need to write code (for declaring MyImages) as shown in Example 11.

Example 11:

Interfaces Example 11

The output of Example 11 will be:

DisplayImage Implementation!
DeleteImage Implementation!
This is the Open method of BaseIO
ApplyAlpha Implementation!

As you can see in Example 11, all you do is add another comma and the interface name during the MyImages class definition (the part made bold in Example 11). We implement the ApplyAlpha() method within class MyImages and call methods as usual.

Multiple interface implementation is completely acceptable in C# as long as no naming conflicts occur. For example if both interfaces (IPict and IPictManip) have an operation called DisplayImage with the same signature (return types and parameters), we have a problem, since it is not possible to specify which interface we are implementing when we write the implementation for the method DisplayImage().

C# provides us with a solution for this problem by providing Explicit Interface Implementation. We shall discuss this in the next section.

2.   Explicit Interface Implementation

To specify which interface a member function is implementing you need to qualify the member function by putting the interface name before the member name. Getting back to the previous example, consider that the operation DisplayImage() exists in both the interfaces (IPict and IPictManip). This poses a problem of ambiguity. Explicit interface implementation cab be used to solve this problem. The corresponding code will be as shown in Example 12.

Example 12:

Interfaces Example 12

3.   Interface Inheritance

New interfaces can be created by combining together other interfaces. The syntax for this is very similar to that used for inheritance, except that more than one interfaces can be merged to form a single interface.

Suppose you want to merge two interfaces IPict and IPictManip into one interface IPictAll you would need to do the following.

Example 13:

Interfaces Example 13

Leave a comment