I seen code in books where there are members specified as public (and not just default access) in non-public classes. What use could this specification of members as public be in non-public classes be?
Suresh S Nayar wrote:
And i meant, i have seen member methods declared as public in non-public classes.
Well, methods that override public methods in other classes, or that implement interface methods, must be public, even if the class is not. There are many non-public classes that implement interfaces and thus have public methods.
it didn't really strike me earlier that this (overriding public methods from superclasses or implementing an Interface's methods) could be the reason that some members will HAVE to be declared as public, even if a class is not public, effectively restricting the availability of its members to the package's classes and that class's subclasses. Now i am clearer.
Joined: Oct 13, 2005
Restricted to the package and subclasses is protected access.
It is usual for (most) methods to be declared public; they constitute the "public interface" of the class, and ought to be accessible from any code.