Packages in Java-1
Packages in Java-1
Packages in Java-1
Packages In Java
Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. Packages
are used for:
• Preventing naming conflicts. For example, there can be two classes with name Employee in
two packages, college.staff.cse.Employee and college.staff.ee.Employee
• Making searching/locating and usage of classes, interfaces, enumerations and annotations
easier
• Providing controlled access: protected and default have package level access control. A
protected member is accessible by classes in the same package and its subclasses. A default
member (without any access specifier) is accessible by classes in the same package only.
• Packages can be considered as data encapsulation (or data-hiding).
All we need to do is put related classes into packages. After that, we can simply write an import class
from existing packages and use it in our program. A package is a container of a group of related classes
where some of the classes are accessible are exposed and others are kept for internal purpose.
We can reuse existing classes from the packages as many time as we need it in our program.
• Built-in Packages
Built-in packages or predefined packages are those that come along as a part of JDK (Java Development
Kit) to simplify the task of Java programmer. They consist of a huge number of predefined classes and
interfaces that are a part of Java API’s.
Some of the commonly used built-in packages are:
1) java.lang: lang stands for language. The Java language package consists of java classes and interfaces
that form the core of the Java language and the JVM. It is a fundamental package which is useful for
writing and executing all Java programs. Examples are classes, object, String, Thread, predefined data
types, etc. It is imported automatically into the Java programs.
2) java.io: io stands for input and output. It provides a set of I/O streams which are used to read and
write data to files. A stream represents a flow of data from one place to another place.
3) java.util: util stands for utility. It contains a collection of useful utility classes and related interfaces
which implement data structures like LinkedList, Dictionary, HashTable, stack, vector, Calender, data
utility, etc.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the components for graphical user interfaces (like
button,menus etc).
6) java.net: net stands for network. It contains networking classes and interfaces for networking
operation. The programming related to client-server can be done by using this package.
• User-defined packages:
These are the packages that are defined by the user. User-defined packages are those which are
developed by users in order to group related classes, interfaces and sub packages. First we create a
directory (name should be same as the name of the package). Then create the Class inside the directory
with the first statement being the package names.
Here,To put a class into a package, at the first line of code define package p1
Step 2) In next step, save this file as Demo.java
Step 3) In this step, we compile the file using javac command.
Here,the compilation is completed. A class file Demo is created. However, no package is created here.
Step 4) Now we have to create a package, use the command
javac -d Destination_folder file_name.java
This command forces the compiler to create a package.
Here,the "." operator represents the current working directory. If we used ". ." operator it represents
parent directory.
Step 5) When you execute the code, it creates a package p1. When you open the java package p1 inside
you will see the Demo.class file.
Step 6) To execute the code mention the fully qualified name of the class i.e. the package name followed
by the class name –
These are the steps we used to create and access user defined package.
Using a package :
The import is a keyword which is used to make the classes and interfaces of other packages accessible
to the current package. If we use package.*, all the classes and interfaces of this package can be accessed
(imported) from outside the packages.
Let's understand it by simple example program.
Here in this program package com.bca.calculate means it creates subpackage calculate in bca which is
subpackage of com package.
To use that package in another package we have to import that package as follows :
We can also import specific classes of package to other particular package using import
packageName.className, Using this you can only access the declared class of this package.
Here in this example, we have created a package i.e. pack in which two separate class A and class B are
there respectively. When you want to access that class, first you have to import that class. Like this you
can add number of classes inside particular package.
Importing classes from packages :
import keyword is used to import built-in and user-defined packages into your java source file so that
your class can refer to a class that is in another package by directly using its name.
There are 3 different ways to refer to any class that is present in a different package:
1. Using fully qualified name (But this is not a good practice.)
If you use fully qualified name to import any class into your program, then only that particular class of
the package will be accessible in your program, other classes in the same package will not be accessible.
For this approach, there is no need to use the import statement. But you will have to use the fully
qualified name every time you are accessing the class or the interface, which can look a little untidy if
the package name is long.