Hi,
What I am having trouble understanding is why we do this
Interfaces are a powerful programming tool because they let you separate the definition of objects from their implementation. Interfaces and class inheritance each have advantages and disadvantages, and you may end up using a combination of both in your projects.
There are several other reasons why you might want to use interfaces instead of class inheritance:
-
Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.
-
Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
-
Interfaces are better in situations in which you do not have to inherit implementation from a base class.
-
Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.
http://msdn2.microsoft.com/en-us/library/3b5b8ezk.aspx
realise .NET only allows the inheritance of one class but could someone explain how I would inherit two "classes" into one base class using interfaces please
C# supports single inheritance. It can inherit from one class, however it does supports multiple 'interface' inheritance:
Eg:
Single Inheritance
public class A
{
public A() { }
}
public class B : A
{
public B() { }
}
Multiple Interface Inheritance
interface IComparable
{
int CompareTo(object obj);
}
interface ISomethingElse
{
int EqualTo();
}
public class Z : IComparable, ISomethingElse
{
public int CompareTo(object obj)
{
// implementation code goes here
}
public int EqualTo()
{
// implementation code goes here
}
}
HTH,
Suprotim Agarwal