Java Reference
In-Depth Information
I want to point out that this directory structure for packages absolutely
must be used. There is no shortcut or simple technique to bypass it. The
bytecode for a class in a package must appear in a directory structure
that matches the package name.
The Access Specifiers
Every member of a class (that is, the fields, methods, and constructors) has an
access specifier that determines who has access to the member. Access specifiers
allow the design of your programs to take into account who should be access-
ing the various attributes and behaviors of an object.
For example, you might want to add a method to your class that performs
repetitive tasks and that is only invoked by the other methods in the class. You
can declare this method private, thereby hiding the method from anyone out-
side the class.
Similarly, you might have a method that needs to be available to any other
objects. Then you should declare this method as public, which allows universal
access to members.
Java provides four levels of access for members of a class:
Public access. Granted using the public keyword, public access is often
referred to as universal access because public members are accessible to
any other object.
Private access. Granted using the private keyword, private is the most
restrictive of the four access specifiers. A private member cannot be
accessed outside of the class.
Protected access. Granted using the protected keyword, a protected
member is accessible to any other class in the same package and also
child classes, no matter which package the child class is in.
Default access. Also referred to as package access, you grant default
access by not using any of the other three access specifiers. (There is
no keyword used to grant the default access.) A member with default
access is accessible to any other classes in the same package.
Notice that protected and default are similar because they grant access
to other classes in the same package. Protected is actually less restricted
than the default because protected also grants access to child classes that
may be outside of the package.
Search WWH ::




Custom Search