Total Pageviews

Monday, 4 July 2011

Package

In simple words , we can say that package are the folders which contains many classes under one package(folder) name. Package is both naming and a visibility control mechanism.First let us deal with naming part , It creates a name space where classes are stored, and it helps us to resolve class name and method name ambiguity.Second part is the Visibility,the classes stored in the package, are inaccessible by the codes outside the package i.e the classes and the member classes are accessible by the members of the same  package only.
We use the package command to define a name space where classes are stored.If we don't use the package command then it is considered as a default package,which has no name.

package ABC;

Java uses file system directories to store the packages.In the above statement .class files of any class declared to be a part of package ABC must be stored in a directory called ABC.We can store more than one file under the same package name. The Package statement only tells us that the specified class belongs to that package.It dose not harm other files from being the part of that same package.

we can create hierarchy of packages.What we have to do is simply separate each package name by the use of period.
package abc[[.efg][.hij]];

example : java.awt.image;
A package declared above must be stored in the java\awt\image in a windows environment.

//create a package
package NSEC;

public class Fees{
String name;
public int roll , cls;
public Fees(String n,int x , int y){
name = n;
roll = x;
cls = y;
}
void show(){
System.out.println(" name " + name +  " class " + cls + " " + " roll_no " + roll );
}}
public class Depart extends Fees{
int months;
long pay;
public Depart(String n , int x, int y , int z, long p){
super(n, x, y );
months = z;
pay = p;
}
void show(){
System.out.println("your fees structure ");
System.out.println( " name " + name + "class " + cls + " " + " roll_no " + roll);
System.out.println("you have to pay " + pay +"$" + " for " + months + " months");
}
}
class accounts{
public static void main(String aregs[]){
Fees sem =  new Fees("BALA" ,46,6);
Depart cse = new Depart("Biplov " , 43, 5, 6 , 28760);
Fees ref;
ref =sem;
ref.show();
ref =cse ;
ref.show();
}
}
In the above program, we have created a package NSEC, and we have stored the .java and .class files in the NSEC directory.

Importing package.
We know that classes and member classes stored in a package are inaccessible outside the package.So, we have to call a package name to access the classes and other files stored in it. This can be done with the help of import statement.
import pkg1[.pkg2](.classname|*);






* is used to import the entire package.
All the standard classes are stored in package called java.The basic language functions are stored in a package inside of a java.lang. Whenever we create a java program ,the java compiler imports the java.lang package implicitly.

//importing the above NSEC package..
import NSEC.*;
class prac{
    public static void main(String args[]){
Depart  a2 = new Depart("biplov" , 43, 5, 6, 28760);
a2.show(); //using the show method defined in the Depart class , which is a part of NSEC package
    }
}







No comments:

Post a Comment