Resources » Articles/Knowledge Sharing » Computer & Technology

Packages In Java


Last Updated:   Category: Computer & Technology    
Author: Member Level: Gold    Points: 10


Are you searching for information on Packages In Java? Then read this article to get Packages In Java detail.



Here is a topic on Packages in Java which many people find difficult to understand.

Notes are compiled from more than 1 book and put in order.


Packages:

Packages are Java’s way of grouping a variety of classes / interfaces together.
Packages act as containers for classes.

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.

Thus, two classes in two different packages can have the same name.

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


Java API Packages
Photobucket

Java API provides a large number of classes grouped into different packages according to functionality.

java.lang: Language support classes. These are the classes that Java compiler itself uses and therefore they are automatically imported. They include classes of primitive types, strings, math functions, threads and exceptions.

java.util: Language utility classes such as vector, hash tables, random numbers, data etc.

java.io: Input/output support classes. They provide 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.

Hierarchical representation of java.awt.package
Photobucket

Accessing the classes from packages

The syntax for accessing the package is:
import packagename.classname;
or
import packagename.*;
Eg:
import java.awt.Color;
import java.awt.*;

Creating own packages

First declare the name of the package using the package keyword followed by a package name.

This must be the first statement in a java source file.

Then define the class.

Declare the package at the beginning of a file using the syntax:
package packagename;

Define the class that is to be put in the package and declare it public.

Create a subdirectory under the directory where the main source files are stored.

Store the listing as the classname.java in the subdirectory created.
Compile the file. This creates .class file in the subdirectory.

Java also supports the concept of package hierarchy. This is done by specifying multiple names in a package statement, separated by dots.
package firstPackage.secondPackage;

This approach allows us to group related classes into a package and then group related packages into a larger package.
Remember to store this package in a subdirectory named firstPackage\secondPackage.

A java package file can have more than one class definitions. In such cases, only one of the classes may be declared public and that class name with .java extension is the source file name.

package firstPackage ;
public class FirstClass
{
//body of the class
}

Here the package name is firstPackage. The class FirstClass is now considered a part of this package. This listing would be saved as a file called

FirstClass.java, and located in a directory named firstPackage. When the source file is compiled, Java will create a .class file and store it in the same directory.

Accessing a package

The import statement can be used to search a list of packages for a particular class.
The general form of import statement for searching a class is as follows:
import package [.package2] [.package3].classname;

here pakcage1 is the name of the top level package, package2 is the name of the package that is inside the package1, and so on.

Finally the explicit class name is specified.

The statement must end with a semicolon (;).

import firstPackage.secondPackage.MyClass ;

After defining this statement, all the members of the class MyClass can be directly accessed using the class name of it objects directly without using the package name.

We can also use another approach as follows:
import packagename.*;

Here packagename may denote a single package or a hierarchy or packages.

The star (*) indicates that the compiler should search this entire package hierarchy when it encounters a class name.

This implies that we can access all classes contained in the above package.

Consider a package named package1 containing a single class ClassA.

package package1;
public class ClassA
{
public void displayA()
{
System.out.println(“Class A”);
}
}

This source file should be named ClassA.java and stored in the subdirectory package1.

Compile this java file.

The resultant ClassA.class will be stored in the same subdirectory.

import package1.ClassA;
class PackageTest1
{
public static void main(String args[])
{
ClassA objectA = new ClassA();
objectA.displayA();
}
}

Now consider another package named package2 containing again a single class as below:
package package2;
public class ClassB
{
protected int m = 10 ;
public void displayB()
{
System.out.println(“Class B”);
System.out.println(“m = “ + m);
}
}
The source file and the compiled file of this package are located in the subdirectory package2.


Importing classes from other packages

import package1.ClassA;
import package2.*;
class PackageTest2
{
public static void main(String args[])
{
ClassA objectA = new ClassA();
ClassB objectB = new ClassB();
objectA.displayA();
objectB.displayB();
}
}

When we import packages it is likely that two or more packages contain classes with identical names:

package pack1;
public class Teacher
{ ……… }
public class Student
{ ……… }

package pack2;
public class Courses
{ ………. }
public class Student
{ ………. }

import pack1.*;
import pack2.*;
class TestPackage
{
public static void main(String args[ ])
{
Student student1 = new Student();
Student student2 = new Student();
Teacher teacher = new Teacher();
Courses course = new Courses();
--------
--------
}

The above problem can be solved by specifying the name of the packages having classes with the same name, while defining the objects.

import pack1.*;
import pack2.*;
class TestPackage
{
public static void main(String args[ ])
{
pack1.Student student1 = new Student();
pack2.Student student2 = new Student();
Teacher teacher = new Teacher();
Courses course = new Courses();
--------
--------
}

Subclassing an imported class

It is also possible to subclass a class that has been imported from another package.
import package2.ClassB;
class ClassC extends ClassB
{
int n = 20;
void displayC()
{
System.out.println(“Class C”);
System.out.println(“m = “ + m);
System.out.println(“n = “ + n);
}
}

class PackageTest3
{
public static void main(String args[])
{
ClassC objectC = new ClassC( );
objectC.displayB();
objectC.displayC();
}
}

Class B
M = 10
Class C
M= 20
N = 20


Did you like this resource? Share it with your friends and show your love!




Responses to "Packages In Java"
Author: Priyanka    04 May 2009Member Level: Silver   Points : 0
Nice explanation


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.