Sunday, October 9, 2016

Java Packages

Java Packages
An article by Prof. Seema Maitrey



Java – Package
Introduction

A package is a collection of related classes. It helps to organize our classes into a folder structure and make it easy to locate and use them. More importantly, it helps improve re-usability.
Each package in Java has its unique name and organizes its classes and interfaces into a separate namespace, or name group.
Although interfaces and classes with the same name cannot appear in the same package, they can appear in different packages.
·         A Java package is a mechanism for organizing Java classes.
·         Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc.
·         It can be defined as a group of similar types of classes, interface, enumeration and sub-packages.
·         Using package it becomes easier to locate the related classes.
·         We can define package using keyword- package.
Creating a Package:-
i.                    include a package command follow by name of package as a first statement of java source file.
ii.                  There can be only one package statement in each source file.
 If package statement not used in source file than it takes default package for all classes, interfaces and enumeration.
 Example:

package MyPackage;
public class Test{
    public static void main(String args[]){
      System.out.println(“Testing package”);
    }
}

 To run this program:
 - Create MyPackage folder in your currently development working directory.
- Compile the source file.
- Put the class file into the directory you have created.
- Execute the program from development directory.

 

Uses of java package

·         Package is a way to organize files in Java.
·         It is used when project consist multiple modules.
·         Package’s access level allows to protect data from being used by the non-authorized classes.
 import keyword:
·         import keyword is used to import built-in and user defined packages into the java source files.  
·         If a class wants to use another class in the same package, the package name does not need to be used.
·         Classes in to the same package use without any import statement.
·         Import statement is kept after package statement.
package MyPackage;
import  java.util.Date;

There are three ways to refer that is present in different package.
1) Using fully qualified name
Example :  class Test extends java.util.Date
 {
   <>  
}
 2) import the class you want to use :
Example:  import java.util.Date;

               class Test extends Date
               {
                <>
               }
 3) import all the classes from the particular package:
Example:
import java.util.*;

class Test extends Date{
  <>
}
Classification of package

Package are categorized into two forms

1.       Built-in Package:-Existing Java package for example java.lang, java.util etc.
  1. User-defined-package:- Java package created by user to categorized classes and interface

Let's see this with an example. We define a class and object and later compile this it in our package p1. After compilation, we execute the code as a java package.
Step 1) Consider the following code,
package p1;         1)
class c1() {          2)
public void m1() 3)
{
System.out.println(“ m1 of c1”);
}
public static void main(String args[])    4)
{ c1 obj=new c1();                                 5)
obj.m1();                                                 6)
}
Here,
1.       To put a class into a package, at the first line of code define package p1
2.       Create a class c1
3.       Defining a method m1 which prints a line.
4.       Defining main method
5.       Creating an object of class c1
6.       Calling method m1
Step 2) In next step, save this file as demo.java

Step 3) In this step, we compile the file.





The compilation is completed. A class file c1 is created. However, no package is created? Let's do that in next step


Step 4) Now we have to create a package, use the command
javac –d . demo.java
This command forces the compiler to create a package.
The "." operator represents the current working directory.
When you execute the code, it creates a package p1. When you open the java package p1 inside you will see c1.class file.
Step 7) Compile the same file using the following code
javac –d .. demo.java
·         Here ".." indicates the parent directory. In our case file will be saved in parent directory which is C Drive(java_prac).

File saved in parent directory when above code is executed.

Step 8) Now let's say you want to create a sub package p2 within our existing java package p1. Then we will modify our code as
package p1.p2
class c1 {
public void m1() {
System.out.println(“m1 of c1”);
}
public static void main(String args[])   
{
c1 obj=new c1();                                  
obj.m1();                                               
}


Step 9) Compile the file
As seen in below screenshot, it creates a sub-package p2 having class c1 inside the package.

Step 10) To execute the code mention the fully qualified name of the class i.e. the package name followed by the sub-package name followed by the class name -
java p1.p2.c1

This is how the package is executed and gives the output as "m1 of c1" from the code file.
Using packages created in earlier assignment (To import package)
Step 1) Copy the code into an editor.
// Using packages created in earlier assignment
package p3;
import p1.*; //imports classes only in package p1 and NOT  in the sub-package p2
class c3{
  public   void m3(){
     System.out.println("Method m3 of Class c3");
  }
  public static void main(String args[]){
    c1 obj1 = new c1();
    obj1.m1();
    p1.p2.c2 obj2 = new p1.p2.c2();
    obj2.m2();
  }
}
 
Step 2) Save the file as Demo2.java . Compile the file using the command javac –d .Demo2.java
Step3) Execute the code using the command java p3.c3
Order of Package Statement
Program coding wise is very simple but is important to know the steps of package creation. While creating User Defined Packages in Java, the order of statements is very important. The order must be like this, else, compilation error.
1.      Package statement
2.      Import statement
3.      Class declaration
If exists, the package statement must be first one in the program. If exists, the import statement must be the second one. Our class declaration is the third. Any order changes, it is a compilation error.

Happy coding!!