Approved by AICTE, Affiliated to JNTUH
Accredited by NAAC-A Grade, NBA& ISO 9001:2015 Certified
DEPARTMENT OF CSE(ARTIFICIAL INTELLIGENCE & MACHINE LEARNING)
SUBJECT: OBEJECT ORIENTED PROGRAMING USING JAVA
Seminar on
PACKAGES
by
22VE1A05H9S---Nandhini
Guided by: DR. A.SWATHI
Packages:
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined
package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util,
sql etc.
Advantages of Packages:
• Java package is used to categorize the classes and interfaces so that they can be
easily maintained.
• Java package provides access protection.
• ava package removes naming collision.
Finding packages and classpath:
A package can be accessed in 3 ways:
1. Using package name: By default the java runtime system uses
the current working directory as its starting point. Thus if your
package is in a subdirectory of the current directory it will be
found.
2. Specifying directory path: you can specify a directory path or
paths by setting the classpath environmental variable.
3. By using fully qualified name: we can use the classpath option
with java & javac to specify the path to one classes
Creating a package:
•Choose the name of the package
•Include the package command as the first line of code in your Java Source
File.
•The Source file contains the classes, interfaces, etc you want to include in the
package
•Compile to create the Java packages
Compiling a package:
javac –d . Packagename.java
Executing a package:
java packagename.programname
Accessing a package:
We can access the package in two ways
(i). By using import statement: If you 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.
(ii). By using fully qualified path: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.