YuWebdesign



Package Structure in Java

By YuwebDesign

Package (Package Structure) in Java

Package in Java is a mechanism to encapsulate a group of related classes, sub packages and interfaces together.

These classes will all be related in some way, e.g.:

  1. belonging to the same category
    e.g., they might all have to do with a specific application;
  2. having similar functionality,
    e.g., they perform a specific set of tasks.

A package can contain the following types:

  1. classes,
  2. interfaces,
  3. enumerations,
  4. annotation types.

Packages help to:

  1. Avoid naming conflicts
    A Java package organizes Java classes into namespaces,
    providing a unique namespace for each type it contains.

    We can define two classes with the same name in different packages to avoid name collision.
    E.g., there can be two classes with name Employee in two packages,
    college.staff.cse.Employee and college.staff.ee.Employee

  2. Write a better maintainable code
  3. Reusability
    Define reusable code in form of classes inside a package.
    After that we can simply import a class from existing packages and use it in our program.
    We can reuse existing classes from the packages as many times as we need it in our program.
  4. Better Organization makes it easier to search for/locate and use classes, interfaces, enumerations, and annotations.

    For several hundreds of classes, it is always required to group the similar types of classes in a meaningful package name so that you can organize your project better and when you need something you can quickly locate it and use it, which improves the efficiency.

  5. Controlled access:
    public, protected and default have package level access control.

    Public members and classes are visible everywhere
    and private members are visible only in the same class.

    Classes in the same package can access each other’s package-private and protected members.

    A protected member is accessible by
    classes in the same package
    and its subclasses
    (classes in other packages that are subclasses of the declaring class).

    A default member (package-private) is accessible by
    classes in the same package only.

  6. Packages can be considered as data encapsulation (or data-hiding).
  7. Java packages can be stored in compressed files called JAR files, allowing classes to be downloaded faster as groups rather than individually.
Package naming conventions

The Java Language Specification establishes package naming conventions
to avoid the possibility of two published packages having the same name.

The naming conventions describe how to create unique package names,
so that packages that are widely distributed will have unique namespaces.

This allows packages to be separately, easily and automatically installed and catalogued.

Packages are usually defined using a hierarchical naming pattern,
levels in the hierarchy are separated by periods (., pronounced “dot”).

Although packages lower in the naming hierarchy
are often referred to as “subpackages
of the corresponding packages higher in the hierarchy,
there is almost no semantic relationship between packages.
E.g., java.util.Scanner
→ java is a top level package
→ util is a sub package
→ Scanner is a class which is present in the sub package util.

Packages are named in reverse order of domain names,
(highest hierarchy on the left):

  1. top level domain name of the organization
  2. .(dot)
  3. organization’s domain
  4. .(dot)
  5. any subdomains
  6. .(dot)
  7. organization can then choose a specific name for its package
  8. .(dot)
  9. subsequent components of the package name
    (vary according to an organization’s own internal naming conventions)

You can visualize a package as a folder/sub-folder structure in a file directory.

E.g, if a package name is college.staff.cse,
then there are three directories, college, staff and cse;
cse is located in staff and staff is located in college
(think of it as college/staff/cse in terms of folders in a file directory).
i.e., org.tarima.yuwebdesign
(think of it as org/tarima/yuwebdesign in terms of folders in a file directory).

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.

Types of Java packages
    Packages are divided into two categories:

  1. Built-in Packages (packages from the Java API)
  2. User-defined Packages (create your own packages)

Leave a Reply or Comment

Your email address will not be published. Required fields are marked *