| Author: R O B I N 07 Oct 2008 | Member Level: Gold | Rating: Points: -20 |
you can use the example like:
interface IMyInterface { void MethodToImplement(); }
class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); }
public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } }
It is an simple example to implement the interface
|
| Author: loki 07 Oct 2008 | Member Level: Bronze | Rating: Points: -20 |
Hi,
Interface implementation can be confusing...
I see a feature in C# that can be very confusing. In the example below we have a class (Test) that implements 2 interfaces (I1 and I2). So the logic says that we should have implementations in the class Test for both MyFunction() methods from I1 and I2. BUT! As you see in the example below everything works fine with only 1 implementation. This is not as correct as we expect, because we can't be sure which of the two interfaces is implemented in the MyFunction method in class Test. So the solution is in another example (EXAMPLE2). In Example2 MyFunction() is implemented in class Test for each interface, by using a different method. (I1.MyFunction() -> for I1, I2.MyFunction() -> for I2).
//EXAMPLE 1 using System;
namespace PavelTsekov { interface I1 { void MyFunction(); } interface I2 { void MyFunction(); } class Test : I1,I2 { public void MyFunction() { Console.WriteLine("Guess which interface I represent???!"); } } class AppClass { public static void Main(string[] args) { Test t=new Test(); I1 i1=(I1)t; i1.MyFunction(); I2 i2=(I2)t; i2.MyFunction(); } } }
//EXAMPLE 2 using System;
namespace PavelTsekov { interface I1 { void MyFunction(); } interface I2 { void MyFunction(); } class Test : I1,I2 { void I1.MyFunction() { Console.WriteLine("Now I can say this here is I1 implemented!"); } void I2.MyFunction() { Console.WriteLine("Now I can say this here is I2 implemented!"); } } class AppClass { public static void Main(string[] args) { Test t=new Test(); I1 i1=(I1)t; i1.MyFunction(); I2 i2=(I2)t; i2.MyFunction(); } } }
Loki.
|
| Author: Meetu Choudhary 23 Oct 2008 | Member Level: Gold | Rating: Points: 6 |
Hi,
Interface
A standard way of calling routines and passing data structures. For example, the interface between two layers of code might be expressed in terms of routines that pass and return a particular data structure. Linux's VFS is a good example of an interface.
An interface in the programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final). An interface may never contain method definitions.
As interfaces are implicitly abstract, they cannot be directly instantiated. Object references may be specified to be of an interface type; in which case they must either be null, or be bound to an object which implements the interface.
The keyword implements is used to declare that a given class implements an interface. A class which implements an interface must either implement all methods in the interface, or be an abstract class.
One benefit of using interfaces is that they simulate multiple inheritance.
for more information please visit
http://en.wikipedia.org/wiki/Interface_(Java)
== Thanks and Regards Meetu Choudhary
|