4th Module 1st Chapter
4th Module 1st Chapter
with JAVA
Semester 3
Course Code : BCS306A
By – Ms. Yamini Sahukar. P (Assistant Professor-AI & ML),
Bangalore Institute of Technology, Bengaluru-560004
Module: 4
(PART A)
Packages: Packages, Packages and Member Access, Importing
Packages.
Package
Package in Java is a mechanism to encapsulate a group of classes, sub
packages and interfaces.
Packages are used for:
1. Preventing naming conflicts.
For example there can be two classes with name Employee in two
packages,
college.staff.cse.Employee and college.staff.ee.Employee
2. Providing controlled access: protected and default have package
level access control.
A protected member is accessible by classes in the same package and
its subclasses.
A default member (without any access specifier) is accessible by classes
in the same package only.
3. Packages can be considered as data encapsulation (or data-hiding).
All we need to do is put related classes into packages.
After that, we can simply write an import class from
existing packages and use it in our program.
A package is a container of a group of related
classes where some of the classes are accessible
are exposed and others are kept for internal
purpose.
We can reuse existing classes from the packages as
many time as we need it in our program.
Defining a Package
To create a package is quite easy: simply include a package command as
the first statement in a Java source file.
Any classes declared within that file will belong to the specified package.
The package statement defines a name space in which classes are stored.
This is the general form of the package statement:
package pkg;
pkg is the name of the package
Example: package mypackage;
Java uses file system directories to store packages.
For example, the .class files for any classes you declare to be part of
mypackage must be stored in a directory called mypackage.
The case is significant, and the directory (A file system structure containing
files and other directories is called directories) name must match the package
name exactly.
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.
package pkg1[.pkg2[.pkg3]];
How does the Java run-time system know where to look for packages
that you create?
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.
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
Example: package mypack;
In order for a program to find mypack, 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. 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
C:\MyPrograms\Java
How packages work?
Package names and directory structure are closely
related.
For example if a package name is college.staff.cse,
then there are three
directories, college, staff and cse such that cse is
present in staff and staff is present
inside college.
Also, the directory college is accessible
through CLASSPATH variable, i.e., path of parent
directory of college is present in CLASSPATH. The
idea is to make sure that classes are easy to locate.
A Short Package Example
Cont…
Call this file AccountBalance.java and put it in a directory called mypack. Next, compile
the file. Make sure that the resulting .class file is also in the mypack directory. Then, try
executing the AccountBalance class, using the following command line:
java mypack.AccountBalance
Cont…
Remember, you will need to be in the directory above mypack
when you execute this command. (Alternatively, you can use one
of the other two options described in the preceding section to
specify the path mypack.) As explained, AccountBalance is now
part of the package mypack. This means that it cannot be
executed by itself. That is, you cannot use this command line:
java AccountBalance