Why are implemented methods of an interface can not be private?

8

When I implement an interface in my class, why its implemented methods can not be private / protected / etc?

And another doubt, when I implement an interface method explicitly, why can not this method be public ?

Example:

interface IBaseInterface
{
    void ExampleMethod();
}

public class BaseClass : IBaseInterface
{
    public void IBaseInterface.ExampleMethod()
    {
        Console.WriteLine("BaseClass.ExampleMethod();");
    }
}

Generate error:

  

Error 1 The modifier 'public' is not valid for this item

    
asked by anonymous 17.04.2015 / 00:27

2 answers

8

The purpose of private methods is precisely to hide the implementation of something. The interfaces serve to describe a contract that an API must have. Contracts do not care about implementations. So it does not make sense to have private methods on interfaces.

Put another way, interfaces state that any type implementing it must have those methods because consumers of that type expect those methods to be there, and of course they can be accessed. To oblige a type to have a private method is to go beyond the contract, is to get involved in the implementation detail that only concerns the type concretely implemented. Since the method can not be accessed publicly, it is only a matter of deciding whether that method should exist or not.

Just because all methods of an interface must be public, it does not make sense to put this information because it is already implicit in the interface.

In C # 8 you probably can, but only because the interfaces will have implementation, and the private method can only be called by a method implemented in the interface. Probably it can have protected method also, there the method can be called by the class that implement this interface. When it has been released let's talk about it (ask a question about interfaces with implementation, if it was already released when reading this).

    
17.04.2015 / 00:44
7

Because it does not make sense.

The function of an interface is to expose methods through a contract to other entities and systems. Therefore, there is no need for these other entities to know private methods, because they will not be used by these other entities.

When a class implements an interface, this does not mean that everything that is declared in the interface should be implemented in the class, because this would make it impossible for a class to implement multiple interfaces. For example:

public class MinhaClasse: IMinhaInterface1, IMinhaInterface2 { ... }

In your case, your code might look like this:

interface IBaseInterface
{
    void ExampleMethod();
}

public class BaseClass : IBaseInterface
{
    public void ExampleMethod()
    {
        Console.WriteLine("BaseClass.ExampleMethod();");
    }

    private void AnotherMethodThatDoesntNeedToBeKnown() 
    {
        // Como o método é privado, só a classe precisa acessar.
        // Veja que não há necessidade de ele ser exposto em uma interface.
    }
}
    
17.04.2015 / 00:43