Interface looks like a class, but has no implementation. The only thing it contains is definitions of events, indexers, methods and/or properties. The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined.
Example
//defining first interface
interface Ifirst
{
void sum(int a, int b);
}
//defining second interface which inherits first interface
interface Isecond:Ifirst
{
void sub(int a, int b);
}
//defining class which inherits second interface
class newClass : Isecond
{
//implementing interface defined methods
public void sum(int a, int b)
{
MessageBox.Show("Sum is: " + (a + b).ToString());
}
public void sub(int a, int b)
{
MessageBox.Show("Difference is: " + (a - b).ToString());
}
}
Demonstrating u
//creating instance of class
newClass c = new newClass();
private void btnSum_Click(object sender, EventArgs e)
{
//calling sum() of Ifirst interface
c.sum(Convert.ToInt32(txtNum1.Text), Convert.ToInt32(txtNum2.Text));
}


Leave Comment
1 Comments