Package in Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Packages in java

PACKAGES
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
college.staff.eee.Employee

Making searching/locating and usage of classes, interfaces, enumerations and


annotations easier

19/11/2023 2
PACKAGES

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).

19/11/2023 3
PACKAGES

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

19/11/2023 4
How packages work?
Package names and directory structure are closely related.

For example if a package name is college.staff.cse, then there are three


directories, college, staff and cse such that cse is present in staff and staff is
present inside college.

Also, the directory college is accessible through CLASSPATH variable, i.e.,


path of parent directory of college is present in CLASSPATH.

The idea is to make sure that classes are easy to locate.

19/11/2023 5
Types of packages:

19/11/2023 6
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


LinkedList

19/11/2023 7
Built-in Packages
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.

19/11/2023 8
User-defined packages
These are the packages that are defined by the user. First we create a
directory myPackage (name should be same as the name of the package).
Then create the MyClass inside the directory with the first statement being
the package names.

19/11/2023 9
User-defined packages

19/11/2023 10
User-defined packages

19/11/2023 11
19/11/2023 12
19/11/2023 13
19/11/2023 14
Important points:
Every class is part of some package.

If no package is specified, the classes in the file goes into a special unnamed
package (the same unnamed package for all files).

All classes/interfaces in a file are part of the same package. Multiple files can
specify the same package name.

If package name is specified, the file must be in a subdirectory called name (i.e.,
the directory name must match the package name).

We can access public classes in another (named) package using: package-
name.class-name

19/11/2023 15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy