Java Module 4
Java Module 4
MODULE 4
EPCET Page 1
MODULE 4 PACKAGES
Packages:
❖ A java package is a group of similar types of classes, interfaces and sub-packages.
❖ Keyword is package
❖ Package in java can be categorized in two form, built-in package and user-defined
package.
Built-in Packages
These packages consist of a large number of classes which are a part of Java API.
Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data
types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes which implement data structures like Linked List,
Dictionary and support ; for Date / Time operations.
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: Contain classes for supporting networking operations.
EPCET Page 2
MODULE 4 PACKAGES
User-defined packages
➢ packages that are defined by the user.
➢ keyword called “package” which is used to create user-defined packages
Ex: package Package name;
❖ Advantages:
➢ Java package is used to categorize the classes and interfaces so that they can be
easilymaintained
➢ Making searching/locating and usage of classes easier
➢ Java package provides access protection.
➢ Java package removes naming collision.
➢ Packages can be considered as data encapsulation
Here, we will have the detailed learning of creating and using user-defined packages.
Simple Package example:
package mypack ; //keyword package followed by name of the package
Access Protection:
➢ Java provides many levels of control over the visibility of variables and methods within
classes, subclasses, and packages.
➢ Classes and packages are both means of encapsulating and containing the name space and
scope of variables and methods.
➢ Packages act as containers for classes and other subordinate packages.
➢ Classes act as containers for data and code.
EPCET Page 3
MODULE 4 PACKAGES
• Classes that are neither in the same package nor subclasses
➢ The three access specifiers, private, public, and protected, provide a control way of
access .
➢ Anything declared public can be accessed from anywhere.
➢ Anything declared private cannot be seen outside of its class.
Non-subclasses
in the same NO YES YES YES
package
Subclasses in
different NO NO YES YES
packages
Importing Packages:
➢ The import keyword used to import built-in and user-defined packages.
➢ When a package has imported, we can refer to all the classes of that package using their
name directly.
➢ The import statement must be after the package statement, and before any other
statement.
EPCET Page 4
MODULE 4 PACKAGES
➢ Using an import statement, we may import a specific class or all the classes from a
package.
Let us discuss how to access package from an other package (Access Protection)
There are 3 ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
import package.*
If we use package.* then all the classes and interfaces of this package will be accessible.
The import keyword is used to make the classes and interface of another package
accessible to the current package.
import package.classname
If you import package.classname then only declared class of this package will be accessible.
EPCET Page 5
MODULE 4 PACKAGES
package mypack1; //save One.java in the package mypack1