1

I have read this article which talks about reasons why default implementetion for methods as well as access modifiers in interfaces were allowed beginning with C#8
But I can not understand why do we have to specify public access modifier when implementing a non-public method from an interface in a class, for example?
If i use the same access modifier as in the interface, the method will not be counted as an implemented method in the implementing class. Here is an example

interface aa {
    protected internal void jj();
    internal void jj1();
    protected void jj2();
    private protected void jj3();

    protected internal void ff() { }
    internal void ff1() { }
    protected void ff2() { }
    private protected void ff3() { }

}
class bb:aa {
    public void jj() { }
    public void jj1() { }
    public void jj2() { }
    public void jj3() { }

    public void ff() { }
    public void ff1() { }
    public void ff2() { }
    public void ff3() { }
}

EDIT:

So from your comments I see that I am only overriding the public members, so My visual Studio is tricking me here since it says that I do actually override them? enter image description here

enter image description here

Eric Movsessian
  • 488
  • 1
  • 11
  • you may rather want to use an abstract class in your case. interfaces are meant for public access only. – Welcor Oct 30 '22 at 16:57
  • 1
    Also the interface exposes only `jj`, `jj1`, `ff` and `ff1`. Your class pretends to implement things it cannot. Therefore, the other methods in the class are unrelated to the interface. Try to write `aa a = new bb();` and then you will see which members you can access! – Olivier Jacot-Descombes Oct 30 '22 at 17:00
  • @Sweeper I am sorry. Just edited the screenshot – Eric Movsessian Oct 30 '22 at 20:53
  • @Sweeper, also why would it say that it inherits them? Sorry if the question is a little dumb. – Eric Movsessian Oct 30 '22 at 20:54
  • 3
    I am reopening this question. It is not a duplicate of [this](https://stackoverflow.com/questions/7238575/why-must-an-c-sharp-interface-method-implemented-in-a-class-be-public), because it concerns the relatively new feature of C# that allows access modifiers on interface member declarations. While the reason for "*public interface methods must be implemented by public methods" is answered in that question, that same reason does not apply to non-public methods, which can only be implemented *explicitly*, rather than implicitly through a public method. – Sweeper Oct 30 '22 at 21:01
  • @Servy meh. While your proposed duplicate explains the base rule, OP's confusion stems from later additions to the language. I couldn't find a better dupe so I answered. Edit: thanks, Sweeper. – CodeCaster Oct 30 '22 at 21:01
  • @EricMovsessian That's going a bit off-topic here... Why *wouldn't* you expect them to be inherited? – Sweeper Oct 30 '22 at 21:02
  • @OlivierJacot-Descombes, thank you very much. I did not see your commnet. So you are saying that `jj`, `jj1`, `ff` and `ff1` are indeed implemented by `bb`. So only public, protected internal and internal methods can be exposed and why is that? and why do I have to make them public in the implementing class `bb` since none of those are not public in interface `aa`? – Eric Movsessian Oct 30 '22 at 22:17
  • 1
    Until recently (before C# 8.0), access modifiers were not allowed in interfaces. All members were implicitly public and did not have a body. Everything else is related to the default implementations and has nothing to do with the classic way interfaces work. If you want to learn interfaces, I suggest to first concentrate on the classic interface definition. Drop all the access modifiers and drop all the bodies. – Olivier Jacot-Descombes Oct 31 '22 at 11:03

1 Answers1

1

Because:

Interface members are public by default because the purpose of an interface is to enable other types to access a class or struct.

If you declare a method on an interface, it's meant to be consumed from the outside. Hence public.

Interface member declarations may include any access modifier. This is most useful for static methods to provide common implementations needed by all implementors of a class.

Non-public interface methods were introduced to allow default implementations. Those can be made non-public so implementors of that interface can share the implementation of those methods (as opposed to, say, an abstract class) without exposing those methods to external consumers of said interface.

More reading: C# 8.0 features: Default interface methods.

why do we have to specify public access modifier when implementing a non-public method from an interface in a class

You don't have to though. It's probably a combination of Visual Studio and ReSharper making you think so.

A more concrete example:

interface IFoo
{
    protected void FooProt();
}

Here, we have an interface declaring a bodyless (i.e. non-implemented) protected method. When a class implements this interface, you'll have to implement that method, either explicitly (void IFoo.FooProt() { }) or publicly (public void FooProt()).

The latter makes no sense, because on variables declared as the type of the interface, you can't call the protected method.

But if you extend this interface, you can call the protected method from it:

interface IFooExtended : IFoo
{
    public void FooPub()
    {
        FooProt();
    }
}

And then protected methods in interfaces begin to make sense:

class Foo : IFooExtended
{
    void IFoo.FooProt()
    {

    }
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Dear CodeCaster, thank you very much for your detailed answer. I am using version 17.4.0 of VS as of now but it seems that again it forces me to use public when I am implementening anything but public methods from the interface in my class. – Eric Movsessian Nov 13 '22 at 21:11
  • 1
    Update or uninstall resharper – CodeCaster Nov 13 '22 at 21:29
  • To be honest I have checked it and I do not even have resharper right now. But Thank you for your time I appreciate it very much. – Eric Movsessian Nov 15 '22 at 22:50