Packages
Packages
1 Introduction to Packages
2 Access Protection
3 Importing Packages
Packages
A package (act as Container) is a collection of related java entities (such as classes, interfaces
exceptions, errors and enums), a great way to achieve reusability, can be considered as means to
achieve data encapsulation.
Packages in Java provides a mechanism for partitioning the class name space into more manageable
chunks.
The Package is both a naming and a visibility control mechanism
Removes naming collision: by prefixing the class name with a package name.
Provides access control: Besides public and private, Java has two access control modifiers-
protected and default (that are related to package).
Provides easy maintenance:
Categorize the classes and interfaces so that they can be easily maintained.
Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.
Packages
Package in java can be categorized in two form, built-in package and user-defined
package.
Built-in packages: standard packages which are part of JRE or Java API. Some of the
commonly used built-in packages are:
Defining a Package
To create a package is quite easy: simply include a package statement as the first
statement in a Java source file.
Any classes declared within that file will belong to the specified package.
If the package statement is omitted, the class names are put into the default package,
which has no name, and suitable for short, sample programs but inadequate for real
applications.
Most of the time, for real applications, you will define a package for the code, using the
general form:
package pkgname; // for example, package MyPackage;
Defining a Package
package MyPackage; // creates a package called MyPackage;
1 Java uses file system directories to store packages.
2 For example, the .class files for any classes will be declared as a part of MyPackage and
must be stored in a directory called MyPackage. Remember that case is significant, and
the directory name must match the package name exactly.
3 More than one file can include the same package statement. The package statement
simply specifies to which package the classes defined in a file belong. It does not exclude
other classes in other files from being part of that same package. Most real-world
packages will spread across many files.
1 By default, the Java run-time system uses the current working directory as its starting point. Thus,
if the package is in a subdirectory of the current directory, it will be found there.
Already Discussed in previous slide
3 It is possible to use the -classpath option with java and javac to specify the path to your classes.
java -classpath /home/UserName/Desktop/MyJavaPrograms/ MyPack.AccountBalance
Because of the interplay between classes and packages, Java addresses four
categories of visibility for class members:
1 Non-subclasses in the same package
2 Subclasses in the same package
3 Subclasses in different package
4 Classes that are neither in the same package nor subclasses
Access Protection
The three access modifiers, private, public, and protected, provide a variety of ways to
produce the many levels of access required by these categories.
[ Source: (1) ]
Access Protection
Anything declared public can be accessed from anywhere.
To allow an element to be seen outside the current package, but only to classes that
are subclasses of the class directly, then declare the element as protected.
Access Protection
A non-nested class has only two possible access
levels default and public (others are abstract
and final)
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
An Access Example
// Shows all combinations of the access control modifiers.
// This example has two packages and five classes. This is file Derived.java:
The general form of the import statement: 1 Here pkg1 is the top-level package, and pkg2 is
import pkg1[.pkg2].(classname|*); the subordinate package inside the outer
package separated by a dot (.).
For example:
import //Explicit Date class 2 There is no practical limit on the depth of a
//Entire io package package hierarchy, except that imposed by the
java.util.Date;
file system.
import java.io.*;
Importing Packages
All of the standard Java classes included with Java are stored in a package called java.
The basic language functions are stored in a package inside of the java package called
java.lang (implicitly imported by the compiler for all programs).
This is equivalent to the following line being at the top of all of your programs:
import java.lang.*;
The import statement is optional. The user should use a class name with its its fully qualified
name, which includes its full package hierarchy.
For example:
import java.util.*;
class MyDate extends Date {
}
The same example without the import statement looks like this:
class MyDate extends java.util.Date { // fully-qualified name
Importing Packages
/* when a package is imported, only those items within the /* Here TestBalance imports MyPack and is then able to make use of
package declared as public will be available to non- the Balance class: */
subclasses in the importing code. */ import MyPack.Balance;