Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 Old CS 160 Packages, Access Control
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Packages, Access Control

by admin last modified 2005-05-25 15:41

Packages; Access Control

Packages

Java uses packages to organize the name space for classes and interfaces.  It helps deal with the problems that can occur if several classes have the same name.
  • Two programmers working on different parts of a large project may use the same name for different classes.
  • A programmer may make up a class name the same as a class in one of the java packages.
The problem is that the set of class names forms a global name space.  It would be better to partition the name space in a manner similar to:
  • Hierarchical directories and folders in Unix and Windows
  • Hierarchical structure of internet names (e.g., occs.cs.oberlin.edu)
Java packages are used to:
  • Organize related classes
  • Avoid naming conflicts
  • Provide a higher level of encapsulaton (higher than the class level)

How it works

The statement
package somePackage;
at the beginning of a Java source file makes the file part of somePackage.  The effect is that the name of every class and interface defined in the file is implicitly prefixed by the package name, as in
somePackage.someClass
So even if two different packages contain classes with the same name, there is no conflict, because the full names of the classes are distinct.

Note:  Classes defined in files without a package statement become part of the unnamed package.

This solves the naming conflict problem, but is somewhat cumbersome to use if every name needs a long prefix.  When is it acceptable to refer to a class name without its package name prefix?
  • Code in package A can refer to class names in package A without the prefix.
  • If a package is imported with an import statement, class names from the package can be used without prefix.
The import statement has two forms:
import somePackage.someClass;    //  import a single class from the package
import somePackage.*;   // import all classes from the package

Q: What happens if two packages containing classes with the same name are both imported?

A: You must use the full name of the classes in order to use them.

Package naming conventions

  1. Packages are stored in different directories.
  2. Files in package abc are stored in directory abc; files in package abc.def are stored in directory abc/def; files in package abc.def.ghi are stored in directory abc/def/ghi, etc.
  3. To execute a class named MyClass in package abc.def (that is, abc.def.MyClass)
    • abc should be a subdirectory of the current directory
    • Use the full name of the class in the java command, as in

      java abc.def.MyClass

  4. To compile all the java files in the package abc.def:

    javac abc/def/*.java

  5. Or, change to the def directory and execute the command

    javac *.java

  6. If an environment variable called CLASSPATH is defined, it contains a list of directories.  Java searches these directories for classes.  This is how we get access to java.util, java.awt, etc.
Q:  Suppose we want to create a project consisting of several java source files, and we want all the files to belong to the same package.  How can this be done?

Access Control

What is the precise meaning of the keywords private, public , and protected in Java programs?  What happens if none of these is used in a declaration?

They are used for access control or visibility, indicating which code can use which class members; that is, variables and methods.

Java defines four levels of accessibility for variables and methods:
  1. public members -- can be accessed by any code which can access the class.
  2. protected members -- can be accessed by code in the same class and by code in any subclass.
  3. package members -- can be accessed by code in the same package.  This is the default when no access control is specified.
  4. private members -- can be accessed only by code in the same class.
(One exception:  Interface members are public by default.)




 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: