Friday, February 3, 2012

Introduction to package in java

What is package?

Packages are a beautiful concept in java. It gives facility to user to create there won class Just like java’s pre defined class. User can create and include there won class using simple import statement. It’s gives us the same concept of “class library” in other programming language.In fact it is container of classes. It’s  possible to include both class and interface within package.
Package are containers for classes that are used to keep the class name space compartmentalized. For example,a package allows you to create a class named xyz (any name given by you),which you can store in your own package with out any concern that it will collide with other packages. Package is, a collection of .class files,created by the user or predefined.

Diagram of java package.

Some benefit of Package
1)The classes contained in the packages of other program can easily reused.
2)Packages gives the idea of hiding class. So that other program cannot able to access it.
3)It helps to use existing code again and again. More than one program can access same package in same                time.
4)If two classname is same then simply put two class in two different package. It will protect name collision.

Java API provides a large number of classes grouped them into several packages depending on there functionality.say for example

How to create Package
It is very simple, simply include package command as the first statement in a Java source file.Any classes declared in that file will belong to the specified package.
Package statement defines a name space in which classes are stored. If you omit the package statement, the class names are put into the default package,which has no name space.
          package pkg;
          Here 'pkg ' is the package name.
      package Mypack;
      Mypack is package name.

1.Program to create a package   
                              //, this class will be included in Mypack1
                              package Mypack1;
                              Public class Pack
                                    public int var=90;
                                    public char c='e';
                               public void show()
                                  System.out.println("These R "+var);

 Save the program in the directory with same name as that of package name.
 i.e, Save Mypack1 package in the Mypack1 directory( folder). and save it with class name given,Pack .class

2.Now complie the program:
   But don’t run, as there is no main(), the program will not run. Now after compiling .class file will be created, make sure that it will be in same directory(folder) where package is saved.

3.Accessing a package
We have to use import statement to include the package.Note that the statement end with semicolon .The import statement will come before class definition of source code file.
The syntax is
import packagename;
import packagename.classname ;
If you want to import all class of particular package then put simply * after packagename
import packagename.*;

Example program of package.

Package package1;
Public class classname
    public void display()
   System.out.println(“I am display function”);

Now compile and save it with and store into package1 folder.Make sure the package1 directory is the subdirectory of source code directory.

Now write the following program

import package1.*;
class test
  public static void main(String args[])
     classname cl=new classname();

And the output will be “I am display function”;

Hiding a class into package

Package p1;
public class a
   //Body of a class
class b
  //Body of class b.

The class b is declared privately. So in main source code it is not possible to create object of b class.
Adding more class in package.
1)Define the class and make it public
2)Write package statement with same package name that you have already created.
Package mypack
public class newclass
3)Compile with filename and save the class file in mypack directory.
4)Write the following statement to import both packages.
Import mypack.*;

No comments:

Post a Comment