Search code examples
c#oopinterfaceaccess-modifiersmultiple-interface-implem

Why we can't use access modifier in the methods of class which implements two interfaces consist of same methods?


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 ?


Solution

  • 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();
      }
    }