Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I got the question from Interviewer which is complicated for me pls give me the better solution.

C#
Interface I1
{
    method1()
    method2()
}

Class A : I1
{
   \\ can Access only method1 not method2
}

Class B : I1
{
   \\ can Access only method2 not method1
}


Is That possible???
Posted
Updated 1-Jun-13 23:37pm
v2

You can do it, to certain extent. If you pass a reference to implementing class as the reference to the interface to some code using the interface implementation, the whole interface is of course accessible. However, if you pass the reference to a class (not recommended in most cases), you can implement one method implicitly, and another one explicitly:

C#
class A: I1 {
   public void Method1() { /* ... */ } // implicit, visible through both class and interface references
   void I1.Method2() { /* ... */ } // explicit, visible through interface reference only,
                                   // not through the class reference
}

//...

I1 implementation = new A(/* ... */);
implementation.Method1(); // OK 
implementation.Method2(); // OK

A implementingClassInstance = new A(/* ... */); // same object,
                                                // different kind of reference to it
implementingClassInstance.Method1(); // OK 
//implementingClassInstance.Method2(); // won't compile

Got the idea?

[EDIT]

See also:
http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx[^],
http://msdn.microsoft.com/en-us/library/vstudio/ms173157.aspx[^],
http://msdn.microsoft.com/en-us/library/vstudio/44a9ty12.aspx[^].

—SA
 
Share this answer
 
v5
Comments
Bernhard Hiller 3-Jun-13 2:33am    
Good idea. I often used internal base classes which implemented an interface and added some extra methods for similar purposes (not exactly the way described by the OP).
By the way, there's a typo in your post: the last two lines of code should use implementingClassInstance instead of implementation [thank you very much, fixed — SA].
Sergey Alexandrovich Kryukov 3-Jun-13 2:57am    
Thank you very much, Bernhard.
—SA
Ranjith M 4-Jun-13 10:39am    
Cant understand, Sergey pls brief... give some more references...
Sergey Alexandrovich Kryukov 4-Jun-13 11:52am    
I added relevant references, please see my updated answer after [EDIT], but...

It might be not quite enough for you. The fact you did not yet understand my very simple code sample is somewhat indicative. The references pin-point the topic, but you might need to make a step way back and get some knowledge and culture of some more basic notions: types and instances (object), static vs. instance members, inheritance, and — importantly — run-time vs. compile-time types. Then you can understand the interfaces better.

Anyway, your follow-up questions are welcome.

—SA
Thomas Daniels 4-Jun-13 12:02pm    
Great, my 5!
I also edited your answer, and I commented out "implicit, visible through both ..." and "explicit ..." (to improve the readability).
It is not Possible, to do that task should create interface individually for different class.

C#
Interface I1
       {
           method1()
       }
 
Interface I2
       {
           method2()
       }
       Class A : I1
       {
          \\ can Access only method1 not method2
       }
 
       Class B : I2
       {
          \\ can Access only method2 not method1
       }
 
Share this answer
 
v3

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