You are on page 1of 7

Unit 3 Part 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.

Java API Packages:


Java APl (Application Program Interface) provides a large number of classes grouped into different
packages according to functionality. Most of the time we use the packages available with the the Java
API. Following figure shows the system packages that are frequently used in the programs.

• 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.

Package Naming Convention:


With programmers worldwide writing classes and interfaces using the Java programming language, it
is likely that many programmers will use the same name for different types. For example if we define
a Rectangle class when there is already a Rectangle class in the java.awt package. Still, the compiler
allows both classes to have the same name if they are in different packages. The fully qualified name
of each Rectangle class includes the package name. That is, the fully qualified name of the Rectangle
class in the graphics package is graphics.Rectangle, and the fully qualified name of the Rectangle class
in the java.awt package is java.awt.Rectangle. This works well unless two independent programmers
use the same name for their packages.
1. Package names are written in all lower case to avoid conflict with the names of classes or
interfaces.
2. Companies use their reversed Internet domain name to begin their package names—for
example, com.example.mypackage for a package named mypackage created by a programmer
at example.com.
3. Name collisions that occur within a single company need to be handled by convention within
that company, perhaps by including the region or the project name after the company name (for
example, com.example.region.mypackage).
4. Packages in the Java language itself begin with java. or javax.
5. In some cases, the internet domain name may not be a valid package name. This can occur if
the domain name contains a hyphen or other special character, if the package name begins with
a digit or other character that is illegal to use as the beginning of a Java name, or if the package
name contains a reserved Java keyword, such as "int". In this event, the suggested convention
is to add an underscore. For example:
Creating and accessing user defined package :
A package is a mechanism to group the similar type of classes, interfaces and sub-packages and provide
access control. It organizes classes into single unit. Java also allows you to create packages as per your
need. These packages are called user-defined packages.
To define a package in Java, you use keyword package.
Syntax : package packageName;
Lets consider an example which shows how to create and access a Package step by step :
Step 1) Consider the following code,

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.

Adding class to package:


We can add more classes to a created package by using package name at the top of the program and
saving it in the package directory. We need a new java file to define a public class, otherwise we can
add the new class to an existing .java file and recompile it.
Example :

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.

2. To import only the class/classes you want to use


If you import packagename.classname then only the class with name classname in the package with
name packagename will be available for use.

3. To import all the classes from a particular package


If you use packagename.*, then all the classes and interfaces of this package will be accessible but the
classes and interface inside the subpackages will not be available for use.
The import keyword is used to make the classes and interface of another package accessible to the
current package.

You might also like