Why can’t the implemented methods of an interface be private?

Asked

Viewed 1,138 times

9

When I implement an interface in my class, why your implemented methods cannot be private/protected/etc?

And another question, when I implement an interface method explicitly, why can’t this method be public?

Example:

interface IBaseInterface
{
    void ExampleMethod();
}

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

Generates the error:

Error 1 The Modifier 'public' is not Valid for this item

2 answers

11


The purpose of private methods is precisely to hide the implementation of something. The interfaces serve to describe a contract that an API should have. Contracts don’t care about implementations. So it makes no sense to have private methods on interfaces.

Put another way, interfaces establish that any type that implements it must have those methods because consumers of that type expect those methods to be there, and obviously they can be accessed. Obliging a guy to have a private method is to go beyond the contract, is to get into the implementation detail that only concerns the type concretely implemented. Since the method cannot be publicly accessed, it is only his problem to decide whether that method should exist or not.

Precisely because all the methods of an interface must be public, it makes no sense to put this information because it is already implicit in the interface.

In C# 8 you probably have (some decisions have been postponed to future version, but it already makes sense), but only because the interfaces will have implementation, and the private method can only be called by a method implemented in the interface. You probably have protected method too, then the method can be called by the class that implements this interface. When it has been launched let’s talk about this (ask a question about interfaces with implementation).

7

'Cause it doesn’t 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 declared in the interface should be implemented in the class, because it would make it impossible for a class to implement multiple interfaces. For example:

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

In your case, your code may well 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.
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.