What is a Package? What are its Uses?


Package:

One of the main feature of OOP is its ability to re use the code the code already created. One way of achieving this is by extending the classes and implementing the Interfaces.

If we need to use classes from other programs without physically copying them into the program, this can be accomplished in Java by using packages.

Another way of achieving the reusability in Java is to use packages.

Packages are Java’s   way of grouping a variety of classes and/or Interfaces together. The group is usually done according to functionality. In fact, packages act as ”containers” for classes . 

By organizing our classes into packages we achieve the following benefits:

   ·       The classes contained in the packages of other programs can be easily reused.

   ·      In packages, classes can be unique compared with classes in other packages. That is, two classes in two different packages can have the same name. They may be referred by their fully qualified name, comprising the package name and the class name.

  ·     Package provide a way to “hide” classes thus preventing other programs or packages from accessing classes that are meant for internal use only.

   ·        Packages also provide a way for separating “design” from “coding”. First we can design classes and decide their relationships, and then we can implement the Java code needed .It is possible to change the implementation of any method without affecting the rest of the design.

Java packages are classified into two ways. The first category is known as Java API packages and the second is known as User defined packages.

Java API Packages:

Java API provides a large number of classes grouped into different packages according to functionality. Most of the time we use the packages available with the Java API (Application Programming Interface)



packages



Package Name                                                     Contents

java.lang                                             Language support classes. These are classes that Java          
                                                            compiler itself uses and therefore they are automatically
                                                            imported. They include classes for primitive types, strings,
                                                            math functions, threads and exception.
java.util                                               Language utility classes such as vector, hash tables, random
                                                            numbers, data etc.
java.io                                                 Input/ output support classes. They provides facilities for
                                                            the input and output of data.
java.awt                                              Set of classes for implementing Graphical User Interface.
                                                            They include classes for windows, buttons, lists, menus and
                                                            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 and implementing applets.

Using System Packages:


The packages are organised in a hierarchical structure. The hierarchy is represented by separating the levels with dots(.)

There are two ways of accessing the classes stored in a package. The first approach is to use the fully qualified class name of the class that we want to use. This is done by using the package name containing the class and then appending the class name to it using the dot operator.

Example: java.awt.Colour

In many situations, we might want to use a class in a number of places in the program or we may have to use many of the classes contained in a package. We may achieve this easily as follows.

    import packagename. classname;  
  
    or         

    import packagename.*;

These are known as import statement and must appear at the top of the file, before any class declarations, import is a keyword.

The first statement allows specified class in the specified package to imported.

import java.awt.colour;

The second statement import every class contain in the specified package.

Example: import java.awt;


Will bring all class of java.awt.package

Related

Java 2049761079994810739

Post a Comment

emo-but-icon

item