|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionInterfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference. Before things start getting difficult let me start using simple and short examples to explain the concept of interfaces. Here's a short example that shows you what an interface looks like. P1.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
}
}
interface abc
{
}
OutputHello Interfaces
The above program compiles and runs successfully to produce the desired output. The above program consists of a class P2.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
}
}
interface abc
{
int x;
}
OutputP2.cs(11,3): error CS0525: Interfaces cannot contain fields
Error! Interfaces in C# cannot contain fields i.e variables. The above program declared an integer variable P3.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
}
}
interface abc
{
void xyz()
{
System.Console.WriteLine("In xyz");
}
}
OutputP3.cs(11,8): error CS0531: 'abc.xyz()': interface members cannot have a
definition
This time over we included a function P4.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
}
}
interface abc
{
void xyz();
}
OutputHello Interfaces
The above program compiles and runs successfully to produce the desired output. Finally we made the compiler happy. Interfaces in C# can only contain function declarations. Now let us see interfaces in action. Interfaces are contracts that a class implements in its own way. This means an interface will contain function prototypes and a class that marries this interface will have to take the responsibility of defining the functions whose prototypes are declared by the marrying interface. So its time to perform the marriage between our groom class P4.csclass Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
}
}
interface abc
{
void xyz();
}
OutputP4.cs(1,7): error CS0535: 'Demo' does not implement interface member
'abc.xyz()'
P4.cs(11,8): (Location of symbol related to previous error)
Well, in the above program class P5.csclass Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
}
void xyz()
{
System.Console.WriteLine("In xyz");
}
}
interface abc
{
void xyz();
}
Outputa.cs(1,7): error CS0536: 'Demo' does not implement interface member
'abc.xyz()'.'Demo.xyz()' is either static, not public,
or has the wrong return type.
a.cs(16,8): (Location of symbol related to previous error)
a.cs(7,8): (Location of symbol related to previous error)
Error again! It's not enough for the class P6.csclass Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
xyz();
}
public void xyz()
{
System.Console.WriteLine("In xyz");
}
}
interface abc
{
void xyz();
}
OutputHello Interfaces
In xyz
Bingo! The above program compiles and runs successfully to produce the desired output. As mentioned earlier using interfaces we can invoke functions from different classes using the same interface reference. For this, we need to have different classes to implement the same interface. In the above program our class P7.csclass Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
refDemo.xyz();
Sample refSample = new Sample();
refSample.xyz();
}
public void xyz()
{
System.Console.WriteLine("In Demo :: xyz");
}
}
interface abc
{
void xyz();
}
class Sample : abc
{
public void xyz()
{
System.Console.WriteLine("In Sample :: xyz");
}
}
OutputIn Demo :: xyz
In Sample :: xyz
The above program compiles and runs successfully to produce the desired output. Now that we have two different classes implementing the same interface its time to show you how to invoke functions from different classes using the same interface reference. P8.csclass Demo : abc
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
abc refabc = new Demo();
refabc.xyz();
abc refabc = new Sample();
refabc.xyz();
}
public void xyz()
{
System.Console.WriteLine("In Demo :: xyz");
}
}
interface abc
{
void xyz();
}
class Sample : abc
{
public void xyz()
{
System.Console.WriteLine("In Sample :: xyz");
}
}
OutputIn Demo :: xyz
In Sample :: xyz
The above program compiles and runs successfully to produce the desired output. Inside The following program uses a for loop to invoke the functions of different classes P9.csclass Demo : abc
{
public static void Main()
{
abc [] refabc = {new Demo(), new Sample()} ;
for (int i = 0; i<= 1; i++)
refabc[i].xyz();
}
public void xyz()
{
System.Console.WriteLine("In Demo :: xyz");
}
}
interface abc
{
void xyz();
}
class Sample : abc
{
public void xyz()
{
System.Console.WriteLine("In Sample :: xyz");
}
}
OutputIn Demo :: xyz
In Sample :: xyz
The above program compiles and runs successfully to produce the desired output. P10.csclass Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
abc refabc = new Demo();
refabc.xyz();
}
public void xyz()
{
System.Console.WriteLine("In xyz");
}
public void pqr()
{
System.Console.WriteLine("In xyz");
}
}
interface abc
{
void xyz();
}
interface def
{
void pqr();
}
OutputHello Interfaces
In xyz
The above program compiles and runs successfully to produce a desired output. Class P11.csclass Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
abc refabc = new Demo();
refabc.xyz();
refabc.pqr();
}
public void xyz()
{
System.Console.WriteLine("In xyz");
}
public void pqr()
{
System.Console.WriteLine("In xyz");
}
}
interface abc
{
void xyz();
}
interface def
{
void pqr();
}
OutputP11.cs(9,5): error CS0117: 'abc' does not contain a definition for 'pqr'
Error! An attempt to invoke P12.csclass Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
abc refabc = refDemo;
refabc.xyz();
def refdef = refDemo;
refdef.pqr();
}
public void xyz()
{
System.Console.WriteLine("In xyz");
}
public void pqr()
{
System.Console.WriteLine("In pqr");
}
}
interface abc
{
void xyz();
}
interface def
{
void pqr();
}
OutputHello Interfaces
In xyz
In pqr
The above program compiles and runs successfully to produce a desired output. Class P13.csclass Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
abc refabc = refDemo;
refabc.xyz();
def refdef = refDemo;
refdef.xyz();
}
public void xyz()
{
System.Console.WriteLine("In xyz");
}
}
interface abc
{
void xyz();
}
interface def
{
void xyz();
}
OutputHello Interfaces
In xyz
In xyz
The above program compiles and runs successfully to produce a desired output. Both the interfaces P14.csclass Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
abc refabc = refDemo;
refabc.xyz();
def refdef = refDemo;
refdef.xyz();
}
public void abc.xyz()
{
System.Console.WriteLine("In abc.xyz");
}
public void def.xyz()
{
System.Console.WriteLine("In def.xyz");
}
}
interface abc
{
void xyz();
}
interface def
{
void xyz();
}
Outputa.cs(13,15): error CS0106: The modifier 'public' is not valid for this item
a.cs(18,15): error CS0106: The modifier 'public' is not valid for this item
Bummer! We used the fully qualified name and we got an error. That's because when we use fully qualified names for functions whose prototypes are a part of interfaces, the compiler doesn't need decorators like public. So we decided to remove the access specifier public from the above program. P15.csclass Demo : abc, def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
abc refabc = refDemo;
refabc.xyz();
def refdef = refDemo;
refdef.xyz();
}
void abc.xyz()
{
System.Console.WriteLine("In abc.xyz");
}
void def.xyz()
{
System.Console.WriteLine("In def.xyz");
}
}
interface abc
{
void xyz();
}
interface def
{
void xyz();
}
OutputHello Interfaces
In abc.xyz
In def.xyz
The above program compiles and runs successfully to produce a desired output. A fully qualified naming system allows us to define interfaces having same function prototypes. In the above example, interface P16.csclass Demo : def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
def refdef = refDemo;
refdef.xyz();
refdef.pqr();
}
public void xyz()
{
System.Console.WriteLine("In xyz");
}
public void pqr()
{
System.Console.WriteLine("In pqr");
}
}
interface abc
{
void xyz();
}
interface def : abc
{
void pqr();
}
OutputHello Interfaces
In xyz
In pqr
The above program compiles and runs successfully to produce a desired output. Interfaces support inheritance. Interface What do you think of the fully qualified names of P17.csclass Demo : def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
def refdef = refDemo;
refdef.xyz();
refdef.pqr();
}
void def.xyz()
{
System.Console.WriteLine("In xyz");
}
void def.pqr()
{
System.Console.WriteLine("In pqr");
}
}
interface abc
{
void xyz();
}
interface def : abc
{
void pqr();
}
OutputP17.cs(12,8): error CS0539: 'def.xyz' in explicit interface declaration is
not a member of interface
P17.cs(29,11): (Location of symbol related to previous error)
P17.cs(1,7): error CS0535: 'Demo' does not implement interface member
'abc.xyz()'
P17.cs(26,8): (Location of symbol related to previous error)
Bummer! The prototype of function P18.csclass Demo : def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
def refdef = refDemo;
refdef.xyz();
refdef.pqr();
}
void abc.xyz()
{
System.Console.WriteLine("In xyz");
}
void def.pqr()
{
System.Console.WriteLine("In pqr");
}
}
interface abc
{
void xyz();
}
interface def : abc
{
void pqr();
}
OutputHello Interfaces
In xyz
In pqr
The above program compiles and runs successfully to produce a desired output. But there are some pitfalls when using the fully qualified function names. P19.csclass Demo : def
{
public static void Main()
{
System.Console.WriteLine("Hello Interfaces");
Demo refDemo = new Demo();
refDemo.xyz();
refDemo.pqr();
}
void abc.xyz()
{
System.Console.WriteLine("In xyz");
}
void def.pqr()
{
System.Console.WriteLine("In pqr");
}
}
interface abc
{
void xyz();
}
interface def : abc
{
void pqr();
}
OutputP19.cs(7,5): error CS0117: 'Demo' does not contain a definition for 'xyz'
P19.cs(8,5): error CS0117: 'Demo' does not contain a definition for 'pqr'
The above program fails to clear the compilation hurdle.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||