Multiple interface implementation

R

Raj

While implementing multiple interfaces having same method name, in the
derived class without explicit implementation, if only one method is
implemented like the below way, whether the .NET CLR will refer to interface
one or two? Since this leads to ambiguity, why is the .NET CLR not throwing
any warning or error??

Code snippet for observation:

interface int1
{
void test()
}

interface int2
{
void test()
}

class Program:int1,int2
{
public void test()
{
Console.WriteLine("test");
}

pubic static void Main()
{
Program p=new Program();
p.test();
Console.Read();
}
}

Thank you

Regards
Raj
 
P

Peter Duniho

Raj said:
While implementing multiple interfaces having same method name, in the
derived class without explicit implementation, if only one method is
implemented like the below way, whether the .NET CLR will refer to interface
one or two? Since this leads to ambiguity, why is the .NET CLR not throwing
any warning or error??

What do you find ambiguous about it? What does it mean to "throw a
warning"? Why would the CLR throw an error in any case? Isn't your
question about compile-time correctness, not run-time correctness?

Is there anything at all about your question that you cannot determine
empirically just by trying the code? If so, it would help if you would
clarify. As stated, your question doesn't make much sense.

Pete
 
H

Harlan Messinger

Raj said:
While implementing multiple interfaces having same method name, in the
derived class without explicit implementation, if only one method is
implemented like the below way, whether the .NET CLR will refer to interface
one or two? Since this leads to ambiguity, why is the .NET CLR not throwing
any warning or error??

Code snippet for observation:

interface int1
{
void test()
}

interface int2
{
void test()
}

class Program:int1,int2
{
public void test()
{
Console.WriteLine("test");
}

pubic static void Main()
{
Program p=new Program();
p.test();
Console.Read();
}
}

In your Main method you have a Program object referenced through a
variable of type Program, and you are calling a method "test" defined in
the Program class. The interfaces have nothing to do with this example,
because you aren't relying on the fact that Program implements either
interface. Your question doesn't even come up in connection with this
example.

If you had this instead:

pubic static void Main()
{
Program p=new Program();
int1 i1 = p;
int2 i2 = p;
i1.test();
i2.test();
Console.Read();
}

THEN you'd have an example that would lead to your question. The answer
is that Program.test is the implementation of both int1.test and
int2.test. Neither interface "cares" that the method is also
implementing a method of the same name from *another* interface.

And that's pretty much what explicit implementation is for, I believe:
for cases where a class implements two interfaces that specify methods
of the same name and signature and yet that are supposed to serve two
different purposes.
 
J

JTC^..^

While implementing multiple interfaces having same method name, in the
derived class without explicit implementation, if only one method is
implemented like the below way, whether the .NET CLR will refer to interface
one or two? Since this leads to ambiguity, why is the .NET CLR not throwing
any warning or error??

Code snippet for observation:

interface int1
{
void test()

}

interface int2
{
void test()

}

class Program:int1,int2
{
   public void test()
   {
    Console.WriteLine("test");
   }

   pubic static void Main()
   {
        Program p=new Program();
        p.test();
        Console.Read();
   }

}

Thank you

Regards
Raj

It's all a question of how you would like to "interface" with your
class. I agree that your question doesn't make much sense. Are you
doing an exercise to understand interfaces or do you have a real world
example?
 
M

Markus Schaber

Hallo,

Raj said:
While implementing multiple interfaces having same method name, in the
derived class without explicit implementation, if only one method is
implemented like the below way, whether the .NET CLR will refer to interface
one or two? Since this leads to ambiguity, why is the .NET CLR not throwing
any warning or error??

When you don't explicitly implement an interface, the method is added
to the class signature and to all matching interface methods which are
not explicitly implemented.
Code snippet for observation:

interface int1
{
void test()
}

interface int2
{
void test()
}

class Program:int1,int2
{
public void test()
{
Console.WriteLine("test");
}

pubic static void Main()
{
Program p=new Program();
p.test();
Console.Read();
}
}

In your case, the test() metod is visible via the class directly (as
you call it in your code) and via both interfaces.


Gruss,
Markus
 
F

Family Tree Mike

Raj said:
While implementing multiple interfaces having same method name, in the
derived class without explicit implementation, if only one method is
implemented like the below way, whether the .NET CLR will refer to interface
one or two? Since this leads to ambiguity, why is the .NET CLR not throwing
any warning or error??

Code snippet for observation:

interface int1
{
void test()
}

interface int2
{
void test()
}

class Program:int1,int2
{
public void test()
{
Console.WriteLine("test");
}

pubic static void Main()
{
Program p=new Program();
p.test();
Console.Read();
}
}

Thank you

Regards
Raj


It doesn't appear to lead to ambiguity. Run the code with this mod to
your main:

public static void Main()
{
Program p = new Program();
p.test();

((int1)p).test();
((int2)p).test();

Console.Read();
}
 
H

harborsparrow

Interfaces do not work like inheritance. Having a method called
"test" in an interface just means that you promise to implement a
method called "test", and the implementation class does satisfy that
requirement for both interfaces just by providing the "test" method
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top