10 Packages Java
10 Packages Java
Vedprakash Sharma
Assistant Professor
IT Dept.
ADGITM
2
INTRODUCTION
A Package is a collection of related Java entities (such
as classes, interfaces, folders/subpackages) to provide
access protection and name space management.
Packages act as “containers” for related java type
files.
A package provides a mechanism for grouping a variety
of similar types of classes, interfaces and sub-
packages.
Example of existing packages:
Java.util
Java.awt
3
ADVANTAGE OF JAVA PACKAGE
1) Java package is used to categorize the classes and
interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
com.zzz.Circle and com.yyy.Circle are two distinct
classes.
4) Packages hide the classes & interfaces in a
separate subdirectory, so that accidental deletion
of classes & interfaces will not take place.
5) Reusability
4
TYPES OF PACKAGES
1. Built-in package
These are also known as System Packages or Java
API.
such as java, lang, awt, javax, swing, net, io,
util, sql etc.
2. User-defined package
The users of the Java language can also create
their own packages. They are called user-defined
packages.
User defined packages can also be imported into
other classes & used exactly in the same way as
the Built in packages.
5
BUILT-IN PACKAGE
As there are built in methods , java also provides inbuilt
packages which contain lots of classes & interfaces. These
classes inside the packages are already defined & we can use
them by importing relevant package in our program.
6
Java
7
JAVA.AWT.COLOR;
JAVA.AWT.GRAPHICS;
java
Package
awt
containing awt
package
Color
Package
containing classes
Graphics
a
Image
8
BUILT-IN PACKAGE EXAMPLES
java.lang
Language Support classes. These are classes that java compiler itself uses & therefore
they are automatically imported. They include classes for primitive types, strings,
maths function, threads &exception.
java .util
Language Utility classes such as vector, hash tables ,random numbers, date etc.
java.io
Input /Output support classes. They provide facilities for the input & output of data
java.awt
Set of classes for implementing graphical user interface. They include classes for
windows, buttons, list, menus & so on.
java.net
Classes for networking. They include classes for communicating with local computers
as well as with internet servers.
java.applet
Classes for creating & implementing applets.
9
ACCESS THE BUILT IN PACKAGE
There are three ways to access the package
from outside the package.
1. fully qualified name.
2. import package.Classname;
3. import package.*;
10
USER DEFINED PACKAGES
Vehicle
Car Bike
11
USER DEFINED PACKAGES
To create a user defined package the following steps should be involved :-
1. Declare the package at the beginning of a file using
syntax :- package packageName;
2. Define the class that is to be put in the package & declare it public.
3. Create a subdirectory under the directory where the main source files are
stored.
4. Store the listing as the classname.java file in the subdirectory created.
5. Compile the file. This create .class file in the subdirectory.
6. Run the file.
12
CONTINUED…
1. Creating User Defined Packages
Syntax :
package packageName;
public class className
{
-------------
// Body of className
------------
}
Package declaration statement must be the first line in the program
Package name should be unique and in lowercase.
How to compile java package
If you are not using any IDE, you need to follow the syntax given below:
javac -d directory javafilename
The -d is a switch that tells the compiler where to put the class file i.e. it
represents destination. The . represents the current folder.
13
EXAMPLE
package vehicle;
public class Bmw{
public void display()
{
System.out.println(“Hello from Bmw class");
}
}
compile
javac -d . Bmw.java
Execute
java vehicle.car.Bmw
Result
Hello from Bmw class
14
CONTINUE… IN THE ABOVE EXAMPLE
■ vehicle is the name of the package.
■ The class Bmw is now considered as a part of this
package.
■ This listing would be saved as a file called Bmw.java &
located in a directory named vehicle.
■ When the source file is compiled, java will create a .class
file & store it in the same directory.
■ The .class files must be located in a directory that has
the same name as the package & this directory should be
a subdirectory of the directory where classes that will
import the package are located.
15
ACCESS THE PACKAGE
There are three ways to access the package
from outside the package.
1. fully qualified name.
2. import package.Classname;
3. import package.*;
16
1) 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.
18
EXAMPLE OF USER DEFINED PACKAGE
//save by Demo2.java
import vehicle.Bmw;
class Demo2{
public static void main(String args[])
{
Bmw obj = new Bmw();
obj.display();
} >Javac –d . Bmw.java
} >Javac Demo2.java
>Java Demo2
19
3) IMPORT PACKAGE.*;
If you use package.* then all the classes and interfaces of this package will be
accessible but not sub-packages.
Used only when there are multiple classes in a package
//save by Demo3.java
import vehicle.*;
class Demo2{
public static void main(String args[])
{
Bmw obj = new Bmw();
obj.display();
}
}
20
THANK YOU