Inheritance
Article Index
Inheritance
Polymorphism
Interfaces

Interfaces and multiple inheritance

Before we leave the topic of inheritance it is worth considering the potentially confusing idea of an ‘interface’.

Banner

Superficially an interface is like a class in that it can be inherited. The C# syntax is even very similar. For example to define an interface with a single method you would write:

public interface IMyInterface
{
void MyMethod(int value);
}

An interface can only define methods and not fields or properties. IMyInterface looks like a class and indeed it can be inherited. For example:

public class MyClass:IMyInterface{}

This looks a lot like simple inheritance but - and this is the important difference – MyClass doesn’t inherit any implementation details from IMyInterface. That is, if you try to create a MyClass object without doing anything about the Interface:

MyClass MyObject=new MyClass();

you can’t call MyMethod using:

MyObject.MyMethod

In fact you can’t even create MyObject unless you write some code that implements all of the methods defined in the interface.

That is, to make the previous example work you would have to define MyClass as:

public class MyClass:IMyInterface{
void IMyInterface.MyMethod(int value)
{
code that does something
}
}

You don’t have to specify the interface method as IMyInterface.MyMethod if there isn’t any chance of a name clash but it’s a good habit to get into. In this case it looks as if MyMethod is being overridden by a new definition provided by IMyInterface.MyMethod. This isn’t really true as there is no implementation of MyMethod provided for you to override!

To be clear:

  • A class that inherits from an interface doesn’t inherit any methods, only method declarations.

What good does this do?

The role of interfaces in object-oriented programming, indeed whether they have a role, is something that can be discussed at length without coming to any strong conclusion.

An interface is very similar to the idea of a pure abstract class. If you declare a class as abstract then any methods that are also declared as abstract don’t have definitions and have to be overridden by any class that inherits the abstract class – unless it too is declared to be abstract. An abstract class can’t be used to create objects and it only becomes “non-abstract” when all of the abstract methods have been overridden.

You can see that abstract classes allow the definition of an inheritance chain that adds more implementation code at each step. An interface on the other hand demands that every method it declares is implemented the very first time it is inherited – in this sense it’s a much cruder instrument.

The intended use of an interface is to ensure that all classes that inherit from it have the methods that it declares. If a class doesn’t implement all of the interface methods exactly then the class doesn’t compile. You can think of it as a template for a class and its use is to ensure that classes present a uniform and enforced set of methods that can be used with confidence by other objects. Used in this way it is a central part of most modern programming methodologies.

One big difference between class and Interface inheritance is that Interfaces support multiple inheritance. You can force a class to implement two Interface specifications using

public class MyClass:IMyInterface1,
IMyInterface2

This looks like multiple inheritance but as nothing but the method definitions are inherited it isn’t nearly so dangerous or, of course, as useful!

Some other facts worth knowing about interfaces are that a struct can inherit an interface and an interface can inherit from other interfaces. Of course an interface that inherits from another set of interfaces doesn’t have to implement anything and it simply serves to extend or add to the base interfaces.

More Deep C#

Banner

<ASIN:047019135X>

<ASIN:059652773X>

<ASIN:1590598733>

<ASIN:1590599543>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.