We know that the concept reusability is one of the advantages of inheritance and interface. This is done by extending classes and implementing interface. But this is limited for reusing the classes in a program. If we want to use classes from other program without copying physically in a program, then java provides a concept that is package which accomplish this task
In general a package is nothing but a container which contains a variety of classes and interfaces. In other word we can say a package represent a directory which contains classes and interfaces.
For example see the following statements
A package contains classes which can be easily reused in other programs.
Packages are used to make a group by taking related classes and interfaces.
Package hides the classes and interfaces in a separate sub-directory.We can say a package is like a library where classes and interface are like books in that library and can be reused in several times. This reusability is the big advantage in object oriented programming
Package provides access protection.
Java package removes naming collision.
In java there are two types of packages, such as:
There are some packages already available in java of its own. These package contains classes , interfaces and methods to perform any task in a program . This package is also known as java API package. Here I have presented some in built packages of java which are as follows :
When the user is defining the packages called as user defined packages.User defined packages are also imported into other classes.
The general form to create a package is
Here the keyword package is used for creating a package.
When we defining a class then we will normally define under a package. For example
Here the package name is demo and the class Race is consider as a part of this package.
Let us see the following program
D:\>javac -d d:\Silan.java
Compiler does not create package physically in current working directory just with javac command. Packaged classes must be compiled with -d option to create package physically.
For example, javac -d.Silan.java
With this command, compiler creates package p1 with Silan class and place it in current working directory. The functionality of -d is creating package with the name mentioned in java file and moving .class file into that package and finally storing that package in the given path.
With the command Javac -d d:\text Silan.java, compiler creates package p1 with Silan.java in d:\text folder. Text folder must be existed in d drive before this program compilation.Packaged class code changed by compiler
After compilation, compiler replaces class name and its constructor name with its package name.class name. it is called fully qualified name.
We must use package name in executing a packaged class else it leads to Exception.So the command for execution is : java p1.Silan
In the above statement it is convenient that we can write the package name before the class name by using import statement such as:
NOTE: The import statement is used to access a package . The general form of import statement for accessing a class is
import Packagename .classname ;
Now the sourse file wii be saved as vivtech.java and then compiled . The output of compilation is vivtech.class file. Now the source file and compile file wiil be saved in the current java directory of which Pranaya was a sub directory then we run the program and get the output.
Let us see another program for better understanding the concept of package
Now let us see to add another number class silan to the same package viswass
Here we are using multiple import statement such as:
Instead of writing multiple import statement we can write a single import statement as:
Where * represents all the classes of the particular package viswass.