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
interfaceIfirst
{
void sum(int a, int b);
}
//defining second interface which inherits first interface
interfaceIsecond:Ifirst
{
void sub(int a, int b);
}
//defining class which inherits second interface
classnewClass : Isecond
{
//implementing interface defined methods
publicvoid sum(int a, int b)
{
MessageBox.Show("Sum is: " + (a + b).ToString());
}
publicvoid sub(int a, int b)
{
MessageBox.Show("Difference is: " + (a - b).ToString());
}
}
Demonstrating u
//creating instance of class
newClass c = newnewClass();
privatevoid btnSum_Click(object sender, EventArgs e)
{
//calling sum() of Ifirst interface
c.sum(Convert.ToInt32(txtNum1.Text), Convert.ToInt32(txtNum2.Text));
}
Anonymous User
09-Mar-2019Thank You.