sekhar
sekhar

Reputation: 19

how interface know which class method call?

i have one doubt if if we have two classes with same method,object of class doesnt know which method to call this situtation we use interface? but how interface know which class method to call,how to apply interface ,i have some code plz check and tell me?

namespace IntExample
{
    interface Iinterface
    {
     public   void add();
     public     void sub();
    }
    public partial class Form1 : Form,Iinterface
    {
        public Form1()
        {
            InitializeComponent();
        }


        public void add()
        {

            int a, b, c;
            a = Convert.ToInt32(txtnum1.Text);
            b = Convert.ToInt32(txtnum2.Text);
            c = a + b;
            txtresult.Text = c.ToString();



        }
        public void sub()
        {

            int a, b, c;
            a = Convert.ToInt32(txtnum1.Text);
            b = Convert.ToInt32(txtnum2.Text);
            c = a - b;
            txtresult.Text = c.ToString();
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            add();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sub();
        }

        class cl2 : Form1,Iinterface
        {
            public void add()
            {

                int a, b, c;
                a = Convert.ToInt32(txtnum1.Text);
                b = Convert.ToInt32(txtnum2.Text);
                c = a + b;
                txtresult.Text = c.ToString();
            }
        }
    }

}

Upvotes: 0

Views: 56

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799460

An interface is an abstraction, one that allows you to perform polymorphism without the need for inheritance. As such, an "interface variable" holds an instance of a concrete class, and the current instance's class is always used for method lookups by the interface as long as the variable contains that instance.

Upvotes: 1

Related Questions