Packages
Packages are containers for classes. It is just like header files in C++ and is stored in a hierarchical manner. To use a package in a class, we need to import it. There are two types of packages. They are
Uses:
Packages reduce the complexity of the software, because a large number of classes can be grouped into limited number of packages.
Using packages we can hide classes.
We can create classes with same name in different packages.
System Packages:
System packages are packages which are available in the java language. They are
java.lang, java.util, java.io, java.awt, java.net, java.applet.
java.lang:
This package contains the classes for primitive data types, strings, math functions, threads, and exceptions. If we create a class, this package will be automatically imported.
java.util:
This package contains the classes related to vectors, hash tables, random numbers, date etc.
java.io:
This package contains input/output related classes.
java.awt:
This package contains classes related to GUI. The classes contain windows, buttons, lists, menus etc.
Java.net:
This package contains classes for networking.
Java.applet:
This package contains classes related to applet.
Using system packages:
General forms:
Import pack1[.pack2][.pack3][…]].classname;
Where,
Import – keyword
Pack1 – top level package.
Pack2 – second level package
Pack 3 – third level package
Classname – a valid class name in the package.
Eg:
import java.awt.*;
import java.lang.math.*;
Naming conventions:
The rules to be followed while naming a package are:
Every package name should be unique.
The first letter of the name should be a lowercase alphabet. This is to distinguish between classes and packages.
Syntax: java.name
Where java – keyword
Name – name of the package.
Eg: java.awt;
Accessing a package:
Each package contains number of related classes. We can access these classes in our application. There are two methods to access.
a)By specifying the fully qualified class name:
We can access a package by specifying the class path at the time of declaration of our object. General form:
Pack1. pack2. pack n. classname obj;
b)By using import statement:
By using import statement we can either use all classes of a package in our application or a specific class. General form:
Import pack1[.pack2[.pack3[…]]].*;
| Name* : |
|||||
| Email* : |
|||||
| Country* : |
|||||
| Phone* : |
|||||
| Subject* : |
|||||
| Upload Homework : Upload another homework (upto 5 uploads max.)
|
|||||
| Due Date |
Time |
AM/PM |
Timezone |
||
| Instructions |
|||||
|
|||||