Package: A package is a mechanism to group the similar type of classes, interfaces and sub-
packages that provide access control, protection and namespace management. It organizes classes
into single unit.
In Java already many predefined packages are available, used while programming.
For example: java.lang, java.io, java.util etc.
Program Example:
package P1;
public class Class1
{
}
public class Class2
{
}
Types of Packages:
There are two types of packages available in Java.
Built-in packages: Built-in packages are already defined in java API. For example: java.util,
java.io, java.lang, java.awt, java.applet, java.net, etc.
User defined packages: The package we create according to our need is called user defined
package.
Advantage of using packages in Java:
i. Maintenance: Java packages are used for proper maintenance. If any developer newly
joined a company, he can easily reach to files needed.
ii. Re-usability: The classes contained in the packages of another program can be easily reused.
iii. Name Conflicts: Packages help to resolve the naming conflict between the two classes with
the same name. For example, there can be two classes with the name Student in two
packages, university.csdept.Student and college.itdept.Student
iv. Organized: It also helps in organizing the files within our project.
v. Data Encapsulation: They provide a way to hide classes, preventing other programs from
accessing classes that are meant for internal use only.
vi. Access Protection: A package provides access protection. It can be used to provide visibility
control. The members of the class can be defined in such a manner that they will be visible
only to elements of that package.
How to access package from another package?
There are three ways to access the package from outside the package.
i. Import package. *;
ii. Import package.classname;
iii. fully qualified name.
01.Using packagename.*
If we use package.* then all the classes and interfaces of this package will be accessible but not
subpackages.
The import keyword is used to make the classes and interface of another package accessible to
the current package.
Example of package that import the packagename.*
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
OUTPUT:
Hello
02.Using packagename.classname
If we import package.classname then only declared class of this package will be accessible.
Example of package by import package.classname
//save by A.java
package pack;
public class A{
public void msg()(System.out.println("Hello");)
}
//save by B.java
package import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg():
}
}
Output:
Hello
3. Using fully qualified name
If you use fully qualified name then only declared class of this package will be accessible.
Now there is no need to import. But you need to use fully qualified name every time when
you are accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and java.sql
packages contain Date class.
Example of package by import fully qualified name
//save by A. java
package pack;
public class A{
public void msg()(System.out.println("Hello");)
//save by B.java
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A()://using fully qualifiedname
obj.msg0);
}
}
Output:
Hello