Pages

Packages JAVA


A package is a container of classes and interfaces. A package represents a directory that contains related group of classes and interfaces. For example, when we write statemens like: import java.io.*;
Here we are importing classes of java.io package. Here, java is a directory name and io is another sub directory within it. The ‘*’ represents all the classes and interfaces of that io sub directory. We can create our own packages called user-defined packages or extend the available packages. User-defined packages can also be imported into other classes and used exactly in the same way as the Built-in packages. Packages provide reusability. 

General form for creating a package:
  package packagename;   e.g.: package pack;
· The first statement in the program must be package statement while creating a package.
· While creating a package except instance variables, declare all the members and the class itself as public then only the public members are available outside the package to other programs. 

Program 1: Write a program to create a package pack with Addition class.
//creating a package package pack;
public class Addition
{           private double d1,d2;
            public Addition(double a,double b)
            {          d1 = a;                         d2 = b;
            }
            public void sum()
            {          System.out.println ("Sum of two given numbers is : " + (d1+d2) );
            }
}
Compiling the above program:  

The –d option tells the Java compiler to create a separate directory and place the .class file in that directory (package). The (.) dot after –d indicates that the package should be created in the current directory. So, out package pack with Addition class is ready.  

Program 2: Write a program to use the Addition class of package pack.
//Using the package pack import pack.Addition; class Use
{           public static void main(String args[])
            {          Addition ob1 = new Addition(10,20);
                        ob1.sum();
            }
}
Output:


Program 3:  Write a program to add one more class Subtraction to the same package pack. //Adding one more class to package pack: package pack; public class Subtraction
{           private double d1,d2;
            public Subtraction(double a, double b)
            {          d1 = a;                         d2 = b;
            }
            public void difference()
            {          System.out.println ("Sum of two given numbers is : " + (d1 - d2) );
            }
}
Compiling the above program: 


Program 4: Write a program to access all the classes in the package pack. //To import all the classes and interfaces in a class using import pack.*; import  pack.*; class Use
{           public static void main(String args[])
            {          Addition ob1 = new Addition(10.5,20.6);
                        ob1.sum();
                        Subtraction ob2 = new Subtraction(30.2,40.11);
                        ob2.difference(); 
}
}
In this case, please be sure that any of the Addition.java and Subtraction.java programs will not exist in the current directory. Delete them from the current directory as they cause confusion for the Java compiler.  The compiler looks for byte code in Addition.java and Subtraction.java files and there it gets no byte code and hence it flags some errors.
Output:



If the package pack is available in different directory, in that case the compiler should be given information regarding the package location by mentioning the directory name of the package in the classpath. The CLASSPATH is an environment variable that tells the Java compiler where to look for class files to import. If our package exists in e:\sub then we need to set class path as follows:


 We are setting the classpath to e:\sub directory and current directory (.) and   %CLASSPATH% means retain the already available classpath as it is. 

Creating Sub package in a package: We can create sub package in a package in the format:
 package packagename.subpackagename;  e.g.: package pack1.pack2;
Here, we are creating pack2 subpackage which is created inside pack1 package. To use the classes and interfaces of pack2, we can write import statement as:
            import pack1.pack2;

Program 5: Program to show how to create a subpackage in a package.
//Creating a subpackage in a package package pack1.pack2;
public class Sample {   public void show ()
            {                       
System.out.println ("Hello Java Learners");
            }
}
Compiling the above program:



Access Specifier:  Specifies the scope of the data members, class and methods.
· private members of the class are available with in the class only. The scope of private members of the class is “CLASS SCOPE”.
· public members of the class are available anywhere .  The scope of public members of the class is "GLOBAL SCOPE".
· default members of the class are available with in the class, outside the class and in its sub class of same package. It is not available outside the package. So the scope of default members of the class is "PACKAGE SCOPE".
· protected members of the class are available with in the class, outside the class and in its sub class of same package and also available to subclasses in different package also.  
Class Member Access
private
No Modifier
protected
public
Same class
Yes
Yes
Yes
Yes
Same package  subclass
No
Yes
Yes
Yes
Same package non-subclass
No
Yes
Yes
Yes
Different package subclass
No
No
Yes
Yes
Different package non-subclass
No
No
No 
Yes

Program 6: Write a program to create class A with different access specifiers.
//create a package same package same;
public class A
{           private int a=1;             public int b = 2;
            protected int c = 3;       int d = 4;
}
Compiling the above program:



Program 7: Write a program for creating class B in the same package.
//class B of same package 
package same; import same.A;
public class B
{            
public static void main(String args[])
            {           
A obj = new A();
                        System.out.println(obj.a);
                        System.out.println(obj.b);                    System.out.println(obj.c);
                        System.out.println(obj.d);
            }
}

Compiling the above program:



Program 8: Write a program for creating class C of another package.
package another; import same.A; public class C extends A
{           public static void main(String args[])
            {          C obj = new C();
                        System.out.println(obj.a);
                        System.out.println(obj.b);                    System.out.println(obj.c);
                        System.out.println(obj.d);
            }
}

Compiling the above program: