Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: OOP
see below c# code
 
public interface Itest1
{
     int Add();
}
 
public interface Itest2
{
    int Add();
}
 
public class test : Itest1,Itest2
{
    public int Add()
    {
        return 10 ;
    }
}
 
which Add method is implemented in test class
 
is it from Itest1 or Itest2 or both
 
many thanks...
Posted 4-Jul-12 7:07am
Comments
Richard MacCutchan at 4-Jul-12 13:18pm
   
Neither, it's the one in class test. Before asking this question you could easily have tested it for yourself.
Sergey Alexandrovich Kryukov at 4-Jul-12 15:55pm
   
That is true, but I nevertheless answered and even up-voted the question.
I am not quite sure if OP deserves my vote, but I never saw such pathological case (and, frankly, could not even answer this question immediately); and trying to test pathological cases is an important skill; if you understand what do I mean...
--SA
Sergey Alexandrovich Kryukov at 4-Jul-12 13:27pm
   
Vote 5 for the question -- pretty funny one.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need explicit interface implementation. Here is the syntax:
 
interface IFirstTest {int Add(); }
 
interface ISecondTest {int Add(); }
 
class DoubleTest : IFirstTest, ISecondTest {
    int IFirstTest.Add() { return ++value; } //pay attention: no access modifiers!
    int ISecondTest.Add() { value += 2; return value; }
    int value;
}
 
Even though your problem hardly can have practical sense, it illustrates the syntax which has benefits over implicit implementations and can solve real problems.
 
One of such problems is: how two write two indexed properties with the same number of indices of different types in one class? This will not compile:
class MyClass {
    public int this[int index] { /* ... */ } 
    public double this[string index] { /* ... */ } //second one will not compile 
}
The solution is:
class MyClass {
    public int this[int index] { /* ... */ } 
    double MyInterface.this[string index] { /* ... */ } //should not have access modifiers 
}
 
//where
interface MyInterface {
    double this[string index] { /* ... */ }
    // in the comment above, it should be "get;" or "get; set;" or "set;"
}
 
In general case, I would rather advise explicit implementation (even if there are no problems illustrated above); it helps to promote using classes purely through interfaces.
 
[EDIT]
 
To answer the original question, you should have tried to work with your class through both interfaces:
Itest2 first = new test();
Itest1 second = new test();
first.Add();
second.Add();
You would see that in both cases, the same function is called; so, in this sense, you have implemented both interfaces at once. The case of implementing of two identical unrelated interfaces is pathological; I don't have it ever makes any practical sense, but you can see it's not prohibited by the compiler.
 
—SA
  Permalink  
v3
Comments
Sandeep Mewara at 4-Jul-12 14:54pm
   
5! for full detailed explanation.
Sergey Alexandrovich Kryukov at 4-Jul-12 15:22pm
   
Thank you, Sandeep.
--SA
Amol_Joshi at 5-Jul-12 0:28am
   
great explanation!!!
Sergey Alexandrovich Kryukov at 5-Jul-12 0:38am
   
Thank you, Amol.
--SA

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

  Print Answers RSS
0 OriginalGriff 553
1 CPallini 470
2 Sergey Alexandrovich Kryukov 418
3 George Jonsson 231
4 Afzaal Ahmad Zeeshan 194
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,032
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100