Java Package
By Ramakrishna on Sep 23, 2008 in Java Package
Package : A package represents a sub directory where we store some Classes and Interfaces
Also A package is a sub directory that contains a group of Classes and Interfaces
Advantages of Packages :
1) It hides Classes and Interfaces in a subdirectory so that accidental deletion is not possible
2) The Classes of one Package are isolated from the Classes of another Packages this means we can
use same name for the classes in 2 different Packages
3) Packages provide resuability, this make software develop easily. The productivity of a program may increase because of reusability
4) Once a package is created it can be extended (or) a new package can be created
Syntax :
package package-name;
You Must compile the package with the Command javac -d (-d indicates the directory)
## Sample Program of Java Package is
//Creating a package:pack
package pack;
public class Addition
{
double d1,d2;
public Addition(double d1,double d2)
{
this.d1=d1;
this.d2=d2;
}
public void sum()
{
System.out.println(“sum=”+(d1+d2));
}
}
Compile this with the Command
javac -d Addition.java
## Sample 2#
//Using the package-pack
class use
{
public static void main(String args[])
{
pack.Addition obj=new Addition(10,15.5);
obj.sum();
}
}
