Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All....

I am bit confusing in the following code block. Could any one help me in this.

C#
interface i1
    {
         void mult();
    }

    interface i11
    {
        void mult();
    }

    class myClass:i1,i11
    {

     public void mult()
        {
            Console.Write("test");
        }
    }

  class Program
    {
        static void Main(string[] args)
        {
            myClass obj= new myClass();
            obj.mult();
          }
    }


This code executes successfully without any errors and produce output as "test".
My Query in this is How the Compiler has the idea for which interface method it is executing.
for I1 or I11
?
Please suggest on this.

Thanks
Vamsi krishna
Posted

here there are methods with same parameter and return type.
and You are calling method of class not of interface.

"An interface contains only the signatures of methods, properties, events or indexers." - MSDN


So interface is just for signature.
In your case Compiler will only check that the class who implement the interface implements its all methods or not.there is no concern with calling a method of particular interface.

Generally we use interface in terms of polymorphism and to take Parent-Child reference related advantages.

Here i have answer one question which shows the real time example of use of Interface.

In polymorphism what is the practical use of using a parent class reference variable to point to child class object instance?[^]

I hope this may clear your idea.

Hope This Helps.
------------------
Pratik Bhuva
 
Share this answer
 
v2
It doesn't. It calls the same method for both.
If you need separate method, then explicitly implement them.
class myClass:i1,i11
{

 public void i1.mult()
    {
        Console.Write("i1 test");
    }
 public void i11.mult()
    {
        Console.Write("i11 test");
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900