They are containers of classes and
interfaces in Java.
In general packages contains group of
similar things wrapped together. In
below diagram you can see different packages containing different things. One package contains pens and the other
package contains books. We group similar things and keep them in packages
because it will be easy for us to maintain them.
In Java, packages are
used to group similar classes and interfaces together so that it will be easy for
us to maintain them. Other than maintenance, packages in Java have many other
advantages.
Advantage
in using packages:
1. Maintaining
applications having many classes and interface becomes easy by using packages.
2. Sometimes
it will be a requirement/convenience to have two or more classes with same
name. In such situations you can use packages to prevent the collision between
same names of the classes.
3. Packages
provide both naming and access controlling.
Packages
can be of three types in Java:
1. Built-in
Packages: These packages are provided in JDK itself. For example java.lang, java.util, java.awt, etc.
2. Default
Packages: When you don’t specify a name to the packages of a class then that
package is called default package.
3. User-defined
Packages: These kinds of packages are created by user itself to group classes
and interfaces. The below discussion is based on user-defined interfaces.
Note: Now-a-days
every programmer will use IDEs like eclipse, etc to develop java programs and
applications. You can easily create classes, interfaces, packages, etc using
these IDEs.
How to create user-defined packages?
The package command is used to create
user-defined packages, as shown in the general form below.
package
pck1.[.pck2[.pck3]] ;
Here, pck1, pck2
and pck3 are names given to the packages. You can provide your own names.
In the above general form the names
pck2 and pck3 are optional only used when you want to create multi-leveled
packages.
In the below programming example you
can see how a package ‘blog.CJME4U’
is specified to a class X. The package statement is the first line in the
program source file.
package
blog.CJME4U; // blog.CJME4U is the package of class X
public class X
{
int a;
private int b;
void
addMethod(int a,int b)
{
System.out.println();
System.out.println("Am addMethod in class X");
System.out.println("addition result "+(a+b));
}
}
In the windows OS environment, the
package statement ‘package blog.CJME4U’
will created two folders, the blog folder and the CJME4U folder inside the blog
folder. The class X source file is placed within the CJME4U folder.
Access
Controlling Mechanism:
The Java provides access controlling
mechanism by using packages. There are four types of access specifiers in Java,
public,
private, default and protected. Using this access
specifiers and packages together, the visibility of classes and their data
members can be controlled as follows.
1. Anything
that is declared public can be
accessed by any code within the package and also outside the package.
2. Anything
declared private cannot be accessed
outside its class.
3. Members
with no explicit access specifiers i.e. default,
can only be accessed by subclasses and the other classes in the same package.
4. Any
member declared protected can be
accessed within its package by every code and also outside its package by only
the direct subclasses.
Consider the below table to understand
and remember the access controlling easily.
Visibility/Access
Specifiers
|
Public
|
Private
|
Default
|
Protected
|
Same
Class/Within the class
|
YES
|
YES
|
YES
|
YES
|
Same
Package’s Subclass
|
YES
|
NO
|
YES
|
YES
|
SamePackage’s Non-Subclass
|
YES
|
NO
|
YES
|
YES
|
Different
Package’s Subclass
|
YES
|
NO
|
NO
|
YES
|
DifferentPackage’s Non-Subclass
|
YES
|
NO
|
NO
|
NO
|
Note: A
non-nested class can have only two access levels i.e. public and default.
The classes present in the different
packages can be accessed by the following two ways,
1. By
using import command.
2. By
using fully qualified package name of class.
In the below example program you can
see that package ‘myBlog.Core.Java’
of class Y is imported into the class X present in the other package ‘blog.CJME4U’.
Note:
Always read the commented green lines in every program examples mentioned in
CJME4U, they are very important to understand the concepts and programs.
package
myBlog.Core.Java; // package of class Y
public class Y
{
public int z=10;
// variable z is made public so that it is visible outside the
package.
void
addmethod()
{
System.out.println("Add result from class in different package "+(z+10));
}
}
// next class
X starts
package
blog.CJME4U;
import
myBlog.Core.Java.Y; // import is used to access class Y from other package.
public class X
{
int a=20;
int b=30;
void
addMethod(int z)
{
int sum=a+b;
System.out.println("add result "+(sum+z));
}
public static void
main(String[] args)
{
X x1=new
X();
Y y1=new
Y(); // object of class from different package accessed by using
import command
int
a=y1.z=10;// variable of class Y from different package is initialized.
x1.addMethod(a);
}
The above program example is
rewritten by using fully qualified package name of class Y. So that we can
access class Y’s public members within class X present in other package. Only
class X is rewritten with public qualified package name of class Y to access it
within the class X.
package
blog.CJME4U;
import
myBlog.Core.Java.Y;
public class X
{
int a=20;
int b=30;
void
addMethod(int z)
{
int sum=a+b;
System.out.println("add result "+(sum+z));
}
public static void
main(String[] args)
{
X x1=new
X();
// note fully qualified
name is used
myBlog.Core.Java.Y y1=new
Y(); // object of class from different package accessed by using
fully qualified name.
int
a=y1.z=10;// variable of class Y from different package is initialized.
x1.addMethod(a);
}
}
Note:
Always read the commented green lines in every program examples mentioned in
CJME4U, they are very important to understand the concepts and programs.