0

In interfaces why method access specifier is only public why not protected?

interface IPractice {
    void test(); // it will be public
    protected void test2(); // why this is not allowed
}

Can any one explain me this.

1

3 Answers 3

2

The whole point of an interface is that it exposes methods to the outside world so implementation details can be hidden.

What happens inside the interface should not be known to the outside world.

1

Because an Interface is public by nature, if you declare an Interface is because you want to make sure that everyone implements the same methods and such methods are PUBLIC.

Think of an interface as the controls of a car (Steering Wheel, Brake, Clutch...) no matter what, that is ALWAYS visible.

If you want several classes to implement the same method but make it protected, you may want to consider an abstract class instead.

0

An Interface is used to access the functionality of the class which is implementing it so you can assign object of class to the Interface reference. And you can call methods from that reference. So only public functionality can be accessed.

Not the answer you're looking for? Browse other questions tagged or ask your own question.