interface Ij
{
void m();
}
class h : Ij
{
public void i()
{
Console.WriteLine("iiii");
}
public void m()
{
Console.WriteLine("mm");
}
}
class Program
{
static void Main(string[] args)
{
h obj = new h();
obj.i();
obj.m();
Console.ReadLine();
}
In the above Interface program why we need to go for Interface "Ij" ? Directly we can go for "h" know? and that too we cannot create object for interface... what is it's purpose? Please explain me friends....................... I am a learner.
In the above Interface program why we need to go for Interface "Ij" ? Directly we can go for "h" know? and that too we cannot create object for interface... what is it's purpose? Please explain me friends....................... I am a learner.

Posted Sep 28, 2013, 8:10 AM
Jignesh TrivediPosted Sep 25, 2013, 1:16 AM
Interface provides a way to achieve runtime polymorphism. Using the interface we can invoke function from differnent class which has same same interface reference.
suppose in your above example i have another class call "P" also implement from same interface. Now depend on some condition i want to create instance of this "H" and "P" class
class P : Ij
{
public void i()
{
Console.WriteLine("Called i method of P class");
}
public void m()
{
Console.WriteLine("Called m method of P class");
}
}
now in main program
class Program
{
static void Main(string[] args)
{
Ij newObj;
Console.Write("Please input the number");
int l = Convert.Toint32(Console.ReadLine());
if (l>10)
{
newObj = new h();
}
else
{
newObj = new P();
}
newObj.i();
newObj.m();
Console.ReadLine();
}
}
hope you under stand...
Kiran Kumar TalikotiPosted Sep 25, 2013, 1:09 AM
In your program.The compiler will throw an error because the method m() creates an ambiguity.
Interface will contain only function Prototypes and and a class that inherits an interface is responsible for implementing method or functions declared in Interface.
Check This Links:
http://www.c-sharpcorner.com/UploadFile/3d39b4/interface-in-C-Sharp-part-1/