C# Simplified, part 4: Structures, Inheritance and Interfaces - Interfaces
(Page 6 of 7 )
As explained previously, C# does not support multiple inheritance. In order to rectify this drawback, Microsoft introduced interfaces into C#. They are regarded as an alternative to multiple inheritance. If you have experience with Java, you should be right at home. All interfaces should be declared with the keyword Interface. You can implement any number of interfaces in a single derived class. A method declared inside the interface doesn't contain any method definition. Instead, the implementing class should provide functionality for the corresponding method.
In listing 4.6, an interface named InterfaceDemo is declared with two method definitions. The derived class, InterfaceImp, implements these two methods. Inside the Main() method, an instance of the class is created and the methods are called appropriately.
Listing 4.6
using System;
interface InterfaceDemo
{
void display();
void print();
}
class InterfaceImp:InterfaceDemo
{
//Interface method implemented
public void display()
{
Console.WriteLine("display() method implemented");
}
public void print()
{
Console.WriteLine("print() method implemented");
}
public static void Main()
{
InterfaceImp ip = new InterfaceImp();
ip.display();
ip.print();
}
Next: Combining Interfaces >>
More C# Articles
More By Anand Narayanaswamy