Access Specifier for Interface Declaration
Why protected/private acce开发者_开发百科ss specifier cannot be used Interfaces declaration ?
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 {}
}
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.
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.
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.)
精彩评论