Understanding on interface in c#, how to implement interface in c#, what is interface

An article on interface using c#


Category: C# Tags: C#


Interface:

  • Interface is a way to provide communication between user and program classes, methods, structures.
  • Interfaces 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.
  • An abstract class serves as the base class for a family of derived classes, while interfaces are meant to be mixed in with other inheritance trees.

Difference from abstract class:

1. Does not contain any concrete methods
2. Multiple Inheritance is possible with interface
3. Access Specifiers are not supported in Interface 


Lets see with a code:

public interface int1 //CAN NOT MAKE AN INSTANCE OF AN INTERFACE
{
void read(); /* CANT DEFINE ACCESS MODIFIER IN INTERFACE
MEMBERS IT GENRATS ERROR */

void write(object obj);
int status { get; set; }
}
Explanation:

        we use keyword interface for make interface.here we made an interface int1 which have one read method, one write and other is status.we can't use access modifiers in interface and also can not make any instance of interface, like abstract class.

public class Doc1 : Int1
{
private int Status = 0;
public Doc1(string s)
{
Console.WriteLine("create Document with: {0}", s);
}
public void read()
{
Console.WriteLine("Implementing the read() method for Int1");
}
public void write(object o)
{
Console.WriteLine("implementing the write() method for Int1 '{0}'",o);
}
public int status
{
get { return Status; }
set { Status = value; }
}
}

Explanation: Because interface do not force to defined members to declare in inherit child classes.

now we made new class doc1, a constructor and we defined the methods in this which declared earlier in interface.

public class tester1
{
static void Main()
{
Doc1 doc = new Doc1("Test Document");
doc.status = -1;
doc.read();
doc.write("NIKHIL JOSHI");
Console.WriteLine("Document status Doc: {0}", doc.status);
}
}
Explanation: Now we made a tester1 class for run interface , we make a object of doc1 and execute the classes, then:


OUTPUT:


create Document with: Test Document
Implementing the read() method for Int1
implementing the write() method for Int1 'NIKHIL JOSHI'
Document status Doc: -1


**Now for more understanding we have a full sample code below:

Because the main aim of interface is to provide multiple inheritance is shown in this code.

using System;
interface first2 //interface 1
{
void read();
void write();
}

//----------------------------------------------
interface second2 //interface 2
{
void read();
void talk();
}

//--------------------------------------------------
class document9 : first2, second2 //multiple inheritance
{
public document9()
{
Console.WriteLine("constructor...");
}

public void read()
{
Console.WriteLine("first2 read()");
}
void second2.read()
{
/* the explicit implementation method can't have an
access modifier, this is implicity public AND CANT USE VIRTUAL KEYWORD */

Console.WriteLine("second2 read()");
}
public void write()
{
Console.WriteLine("first2 write()");
}
public void talk()
{
Console.WriteLine("second2 talk()");
}
}

//---------------------------------------------------------
class check9 //Main class
{
static void Main()
{
document9 doc9 = new document9();
first2 fr2 = doc9 as first2;
if (fr2 != null)
{
fr2.read();
fr2.write();
}
second2 sec2 = doc9;
if (sec2 != null)
{
sec2.read();
sec2.talk();
}
}
}

Explanation:So you can see multiple inheritance above, just go through the code and comments. you will understand the code.


Output:


constructor...

first2 read()
first2 write()
second2 read()
second2 talk()


Like 0 People
Published on 22 May 2013
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 132
Questions: 9
Given Best Solutions: 9 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x