Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

Access Specifier for Interface Declaration

Why protected/private access specifier cannot be used Interfaces declaration ?

Upvotes: 2

Views: 3001

Answers (4)

user207421
user207421

Reputation: 310903

There is no such thing as an access specifier in Java. The term is 'access modifier'.

(So all those interview questions you see asking what is the difference are nonsense.)

Upvotes: 0

GuruKulki
GuruKulki

Reputation: 26418

There is no sense in having private or protected access specifiers for class or interface because these two makes more sense when it comes to variables or methods for achieving encapsulation of the data.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500515

You can declare a protected class or indeed a private one - but only within another one. At that point it's either visible to classes derived from the outer one (in the case of protected) or not (in the case of private):

public class Outer
{
    private static class NotVisibleToSubclassesOfOuter {}

    protected static class VisibleToSubclassesOfOuter {}
}

Upvotes: 5

Kannan Ekanath
Kannan Ekanath

Reputation: 17601

What does a private class mean? That it cannot be used ever outside this class? Why would you ever have such a class? (How would you run/test it?)

What would a protected class mean? That it can only be seen by classes that extend it. So when you type a class skeleton (some where outside the world) it is not visble but when we say extends the class dynamically becomes visible?

Package protected class makes sense - It is visible to the classes inside the package and not really for public consumption.

Upvotes: 0

Related Questions