0
public interface A
{
     void FirstDemo();
     void SecondDemo();
}
public interface B
{
     void FirstDemo();
     void SecondDemo();
}

 public class Demo : A, B
        {
            void A.FirstDemo()
            {
                Console.WriteLine("This is a function of first method of interface A");
            }
            void A.SecondDemo()
            {
                Console.WriteLine("This is a function of second method of interface A");
            }

            void B.FirstDemo()
            {
                Console.WriteLine("This is a function of first method of interface B");
            }
            void B.SecondDemo()
            {
                Console.WriteLine("This is a function of second method of interface B");
            }
        }

static void Main(string[] args)
        {
            A obj = new Demo();
            obj.SecondDemo();
            B obj1 = new Demo();
            obj1.FirstDemo();
        }

This program works properly. But my confusion is If I implement only one interface in the Demo class then I can give an access modifier in every method of that interface. But when I try to implement both interfaces (In this case: A and B) which consists same methods, within the same class (in this case: Demo class) then I'm not allowed to put public access modifier. why it happens?

Note : If I use single interface and implement it in Demo class, Then I can use access modifier to all methods which is declared in interface. So what's the issue in multiple interfaces with same methods ?

  • 1
    Those are explicit interface implementations. They are implicitly public – knittl Dec 09 '21 at 05:47
  • Does this answer your question? [Why in C# does one need to mention the access modifier for implementation of an interface property?](https://stackoverflow.com/questions/49248902/why-in-c-sharp-does-one-need-to-mention-the-access-modifier-for-implementation-o) – knittl Dec 09 '21 at 05:48
  • Yes, I have gone through this link. But my confusion is If I implement only one interface in the Demo class then I can give an access modifier in every method of that interface. But when I try to implement both interfaces (In this case: A and B) which consists same methods, within the same class (in this case: Demo class) then I'm not allowed to put public access modifier. why it happens? – Prince Gajjar Dec 09 '21 at 06:30
  • 1
    `public void MethodFromInterface()` is different from `void IFace.MethodFromInterface()`: `new Obj().MethodFromInterface()` works in the first case, but does not in the latter. With an explicit interface implementation, you need to call as `((IFace)new Obj()).MethodFromInterface()`. You are not comparing 1 vs 2 interfaces, but rather _implicit_ vs _explicit_ interface implementations – you are mixing two distinct concepts here. Your class even makes this obvious, but your calling main function does not: `((A)new Obj()).FirstDemo()` is a different method than `((B)new Obj()).FirstDemo()`. – knittl Dec 09 '21 at 06:32
  • @knittl I think I should deep dive into this concept. But thank you for drawing my attention... – Prince Gajjar Dec 09 '21 at 06:39
  • FYI it is allowed to handle both interfaces with the same methods. So simply `public void FirstDemo()` etc – Hans Kesting Dec 09 '21 at 06:58

2 Answers2

1

The distinction is not so much about "1 interface vs 2 interfaces", but rather "implicit vs explicit interface implementations". An explanation as for the "why is a visibility modifier forbidden" part of the question, I'll refer to question Why in C# does one need to mention the access modifier for implementation of an interface property?.

(Explicit interface implementations are implicitly public. Yes, you read that correctly.)

C# allows you to implement a single interface both implicitly and explicitly and they become different methods. The static type of the object determines which method to call:

interface IFace {
  void Method();
  void OnlyImplicit();
  void OnlyExplicit();
}

public class Obj : IFace {
  public void Method() {
    Console.WriteLine("implicit implementation");
  }

  void IFace.Method() {
    Console.WriteLine("explicit implementation");
  }

  public void OnlyImplicit() {
    Console.WriteLine("only implemented implicitly");
  }

  void IFace.OnlyExplicit() {
    Console.WriteLine("only implemented explicitly");
  }

  public void Main() {
    Obj o = new Obj(); // or: var o = new Obj();
    IFace i = o;

    o.Method(); // call of implicit impl
    i.Method(); // call of explicit impl

    o.OnlyImplicit(); // call of implicit impl
    i.OnlyImplicit(); // call of implicit impl

    i.OnlyExplicit(); // call of explicit impl

    // compile error, method is implemented explicitly,
    // can only be called when static type is interface;
    // cannot be called when static type is the class' type
    // (this is not so obvious):
    o.OnlyExplicit();
  }
}
knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thank you so much... It's really informative for me...Just one last question, Why did you write ' void IFace.Method() ' instead ' public void IFace.Method() ' ? – Prince Gajjar Dec 09 '21 at 07:18
  • @PrinceGajjar `void IFace.Method()` does not allow a visibility modifier, that's how the C# syntax is defined. For details, read the linked question at the top of the answer – knittl Dec 09 '21 at 07:22
  • okk... Got it... I didn't have any idea about this... Thank you so much for clearing my confusion... It's appreciable. – Prince Gajjar Dec 09 '21 at 07:26
0

In order to use an object you need an interface :-).

You can implement una interface implicitly and the other explicitly.

You can not provide multiple different implementations of the same interface method. Otherwise how the compiler will choice the implementation to use?

Suppose you implicit implements A and explicit B:

var demo = new Demo();
demo.FirstDemo(); // it uses public implementation (Demo class interface) that is A
var demoB = (B)demo;
demoB.FirstDemo(); // it uses B interface
GibbOne
  • 629
  • 6
  • 10