Package in Java
A package is a collection of similar types of classes, interfaces and sub-packages.
Purpose of package:
The purpose of package concept is to provide common classes and interfaces for any
program separately. In other words if we want to develop any class or interface which is
common for most of the java programs than such common classes and interfaces must be place
in a package.
Packages in Java are the way to organize files when a project has many modules. Same like we
organized our files in Computer. For example we store all movies in one folder and songs in
other folder, here also we store same type of files in a particular package for example in awt
package have all classes and interfaces for design GUI components.
Ex: import [Link]
Here:
→ java is a top level package
→ util is a sub package
→ and Scanner is a class which is present in the sub package util
E3Civil_Java-Packages Page 1
Advantage of package
Package is used to categorize the classes and interfaces so that they can be easily
maintained
Application development time is less, because reuse the code
Application memory space is less (main memory)
Application execution time is less
Application performance is enhance (improve)
Redundancy (repetition) of code is minimized
Package provides access protection.
Package removes naming collision.
Type of package
Package are classified into two type which are given below.
1. Predefined or built-in package
2. User defined package
Predefined or built-in package
These are the package which are already designed by the Sun Microsystem and supply as a
part of java API, every predefined package is collection of predefined classes, interfaces and
sub-package.
User defined package
If any package is design by the user is known as user defined package. User defined package
are those which are developed by java programmer and supply as a part of their project to deal
with common requirement.
Rules to create user defined package
package statement should be the first statement of any package program.
Choose an appropriate class name or interface name and whose modifier must be public.
Any package program can contain only one public class or only one public interface but
it can contain any number of normal classes.
Package program should not contain any main class (that means it should not contain
any main())
modifier of constructor of the class which is present in the package must be public. (This
is not applicable in case of interface because interface have no constructor.)
The modifier of method of class or interface which is present in the package must be
public (This rule is optional in case of interface because interface methods by default
public)
Every package program should be save either with public class name or public Interface
name.
E3Civil_Java-Packages Page 2
Compile package programs:
For compilation of package program first we save program with public [Link] and it
compile using below syntax:
Syntax
javac -d . [Link]
Syntax
javac -d path [Link]
Explanations: In above syntax "-d" is a specific tool which is tell to java compiler create a
separate folder for the given package in given path. When we give specific path then it create a
new folder at that location and when we use . (dot) then it crate a folder at current working
directory.
Note: Any package program can be compile but can not be execute or run. These program can
be executed through user defined program which are importing package program.
Example of package program:
Package program which is save with [Link] and compile by javac -d . [Link]
Example:
package mypack;
public class A
{
public void show()
{
[Link]("Sum method");
}
}
E3Civil_Java-Packages Page 3
Import above class in below program using import [Link]
Example
import mypack.A;
public class Hello
{
public static void main(String arg[])
{
A a=new A();
[Link]();
[Link]("show() class A");
}
}
Explanations: In the above program first we create Package program which is save with [Link]
and compiled by "javac -d . [Link]". Again we import class "A" in class Hello using "import
mypack.A;" statement.
Difference between Inheritance and package
Inheritance concept always used to reuse the feature within the program between class to class,
interface to interface and interface to class but not accessing the feature across the program.
Package concept is to reuse the feature both within the program and across the programs
between class to class, interface to interface and interface to class.
Difference between package keyword and import keyword
Package keyword is always used for creating the undefined package and placing common
classes and interfaces.
import is a keyword which is used for referring or using the classes and interfaces of a specific
package.
E3Civil_Java-Packages Page 4