articles

Home / DeveloperSection / Articles / Interfaces

Interfaces

Anonymous User8729 15-Jul-2010

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));
        }
 


Interfaces

  

 


Interfaces


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By