CLASSPATH,
Access Protection
CLASSPATH
• Packages are mirrored by directories.
• This raises an important question:
• How does the Java run-time system know
where to look for packages that you create?
• The answer has three parts.
CLASSPATH-cont...
• First, by default, the Java run-time system uses
the current working directory as its starting
point.
• Thus, if your package is in a subdirectory of
the current directory, it will be found.
CLASSPATH-cont...
• Second, you can specify a directory path or
paths by setting the CLASSPATH environmental
variable.
• Third, you can use the classpath option with
java and javac to specify the path to your
classes
CLASSPATH-cont...
• For example, consider the following package
specification:
package MyPack
• In order for a program to find MyPack, one of
three things must be true.
CLASSPATH-cont...
• Either the program can be executed from a
directory immediately above MyPack, or the
CLASSPATH must be set to include the path to
MyPack, or the classpath option must specify
the path to MyPack when the program is run
via java.
CLASSPATH-cont...
• When the second two options are used, the
class path must not include MyPack, itself.
• It must simply specify the path to MyPack. For
example, in a Windows environment, if the
path to MyPack is
C:\MyPrograms\Java\MyPack
• Then the class path to MyPack is
C:\MyPrograms\Java
CLASSPATH-cont...
• The easiest way to try the examples shown in
this book is to simply create the package
directories below your current development
directory, put the .class files into the
appropriate directories, and then execute the
programs from the development directory.
• This is the approach used in the example
discussed earlier in the session 17.
Quiz Questions
1. Packages are mirrored by directories-justify
2. How does the Java run-time system know
where to look for packages that you create?
3. How you can specify a directory path?
Access Protection
• You learned about various aspects of Java’s
access control mechanism and its access
specifiers.
• For example, you already know that access to
a private member of a class is granted only to
other members of that class.
Access Protection-cont...
• Classes and packages are both means of
encapsulating and containing the name space
and scope of variables and methods.
• Packages add another dimension to access
control.
Access Protection-cont...
• As you will see, Java provides many levels of
protection to allow fine-grained control over
the visibility of variables and methods within
classes, subclasses, and packages.
• Packages act as containers for classes and
other subordinate packages.
• Classes act as containers for data and code.
• The class is Java’s smallest unit of abstraction.
Access Protection-cont...
Because of the interplay between classes and
packages, Java addresses four categories of
visibility for class members:
1. Subclasses in the same package
2. Non-subclasses in the same package
3. Subclasses in different packages
4. Classes that are neither in the same package nor
subclasses
Access Protection-cont...
• The three access specifiers,
private, public, and protected,
provide a variety of ways to produce the many
levels of access required by these categories.
Access Protection-cont...
Table- 9-1
Access Protection-cont...
• Table 9-1 sums up the interactions.
• While Java’s access control mechanism may
seem complicated, we can simplify it as
follows.
• Anything declared private cannot be seen
outside of its class
Access Protection-cont...
• When a member does not have an explicit
access specification, it is visible to subclasses as
well as to other classes in the same package.
• This is the default access.
• If you want to allow an element to be seen
outside your current package, but only to
classes that subclass your class directly, then
declare that element protected.
Access Protection-cont...
• Anything declared public can be accessed
from anywhere.
• Table 9-1 applies only to members of classes.
• A non-nested class has only two possible
access levels: default and public.
• When a class is declared as public, it is
accessible by any other code.
Access Protection-cont...
• If a class has default access, then it can only be
accessed by other code within its same
package.
• When a class is public, it must be the only
public class declared in the file, and the file
must have the same name as the class.
Quiz Questions
1. Classes and packages are both means of
encapsulating and containing the name space
and scope of variables and methods-justify
2.Packages act as containers for classes and
other subordinate packages-justify
3.The class is Java’s smallest unit of abstraction-
analyze this statement.
End of session