Package is
a way of grouping a variety of classes and interfaces together. In java,
package is a way to organize set of related classes and interfaces. In simple
words, a package is similar to different folders in our computer. We keep
images in one folder, songs in other folder and so on. So, we keep set of
related documents in one folder. There can be two classes in two different
package can have same name as two files having same name in two different
folders in our computer.
In
java, we have ability to reuse code by extending classes and implementing
interfaces. But if we have to reuse the code written in another program without
copying the code. This can be achieved by using packages. Java provides a class
library for use in our own application called API or Application Programming
Interface. This API provides large number of classes grouped into different
packages according to their functionality.
Advantages of Package in Java
(1) While making a large project, we have many classes and
interfaces, so it is required to group set of related classes and interfaces
into packages. It provides better organisation and improves efficiency.
(2) We can put two classes of same name in two different
package, so it avoids name collision.
(3) Java package provides reusability of code by using different
classes and interfaces in different packages.
(4) We can't directly access the code of classes and interfaces,
so it provides access protection.
Types of Package in Java
Built-in package
Java
already provides many different packages like java.lang.*, java.io.* known as
built-in packages.
User defined package
We
can also create our own package known as user defined package.
Example of Java Package
package showMe;
public class Show{
public void disp(){
System.out.println("Hello");
}
}
import showMe.Show;
public class Demo{
public static void main(String[] agrs){
Show obj = new Show();
obj.disp();
}
}
Output:
Hello
In this
example, first we create a package namely "showme" using package
keyword. We define a class called "Show" and this class containing
only one method called "disp". While putting a class to a package,
the class must be of public access modifier.
We
can create another class called "Demo", this class imports
"showMe" using import keyword. In Demo class, we create an object of
"Show" class of "showMe" package and called a method called
"disp".
To Compile: javac -d . Show.java
Then
again, javac Demo.java
To run Demo class: java Demo
Example 2:
We
can create a class inside a package while importing another package.
package Package2;
import showMe;
class Example{
public static void main(String[] args){
Show obj = new Show();
obj.disp();
}
}
In this
program, first we write package declaration and then we import another package.
It
is important to know that if we write package declaration statement in our
program, then it must be the first statement of program.
Example 3:
If
you don't want to write import statement, then you can use fully qualified
name.
Greatest.java
package Package1;
public class Greatest{
public void checkGreatest(int a, int b){
if(a>b)
System.out.println(a+" is greater than "+b);
else
System.out.println(b+" is greater than "+a);
}
}
Example.java
class Example{
public static void main(String[] args){
Package1.Greatest obj = new Package1.Greatest();
obj.checkGreatest(10, 20);
}
}
Output:
20 is greater than 10
Sub
package in Java
We
can create a package inside another package. For example, java has also sub
packages like lang, io, net etc.
Example:
Show.java
package Package1.SubPackage1;
public class Show{
public void disp(){
System.out.println("Welcome");
}
}
Example.java
import Package1.SubPackage1.*;
class Example{
public static void main(String[] args){
Show obj = new Show();
obj.disp();
}
}
Output:
Welcome