Packages in Java

Java Packages

What is a Java Package

A package is a collection of classes and the related functions within the classes.
Packages are container for classes that are used to keep the class name space compartmentalized. For example, you can create a class named “Abc” in your package “mypackage” without being concerned to it being colliding with other class also named as “Abc”.  And then you can write your own methods in it for the later use by you or anyone that uses that package.
Packages are stored in a hierarchical way. That means you can create a package inside a package. This is like a box contained inside a box.
Now how to create a package? Well this is simple, you just need to use the keyword ‘package’ followed by the name of the package.
This is the general form of the package statement:
package mypackage;
Here package is the keyword for creating the package called mypackage.
Java uses file system directories to store packages. For example, the .class files for any classes you declare to be part of mypackage must be stored in a directory called mypackage. Remember that case is significant, and the directory name must match the package name exactly.
You can create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period. The general form of a multileveled package statement is shown here:

package pkg1[.pkg2[.pkg3]];

A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as

package java.awt.image
needs to be stored in java/awt/image, java\awt\image, or java:awt:image on your UNIX, Windows, or Macintosh file system, respectively. Be sure to choose your package names carefully. You cannot rename a package without renaming the directory in which the classes are stored.

How to Use the package

Now after you have created the package, you would want to use it in your program. For that you must import the package in your program.
Suppose if you created the above package mypackage, then you can import it by using import statement.
import mypackage.*;
The above import statement will import all the classes inside the mypackage package. If you want to import only specific class, you can import it by specifying the class name instead of *.
import mypackage.MyClass;

EXAMPLE PROGRAM

//creating the package
//save each class of the package in a separate file named as the name of the //class e.g. save this file as “Calculation.java”
package mypackage;
public class Calculation
{
            public int Add(int x, int y)
            {
                        return(x+y);
            }
            public float Add(float x, float y)
            {
                        return(x+y);
            }
            public int div(int x, int y)
            {
                        return(x/y);
            }
            public float div(float x, float y)
            {
                        return(x/y);
            }
}
Now Create another class in which you will be using this package.
//using my package
// save the file as “UsingPackage.java”
import mypackage.Calculation;
import java.util.*;
class UsingPackage
{
            public static void main(String args[])
            {
                        Scanner s = new Scanner(System.in);
                        Calculation c = new Calculation();
                        System.out.println("Enter two Numbers:");
                        int x = s.nextInt();
                        int y = s.nextInt();
                        System.out.println(c.Add(x,y));
                        System.out.println("Enter two Numbers:");
                        float a = s.nextFloat();
                        float b = s.nextFloat();
                        System.out.println(c.Add(a,b));
            }
}

ACCESS PROTECTION

The following table applies only to the members of the class. A class can only have two access modifiers : default and public.

Private
Default
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

We can alter the access protection of the members and the classes in the package according to the need.

3 comments:

  1. u got nice infos on Java. good work!

    ReplyDelete
  2. @Priyanka: Thanks for your support. I am trying to add more relevant posts on the blog and make it more useful to the new java programmers.

    ReplyDelete

Please leave your valuable comments here...