Packages in Java


 Packages

 - The main feature of java is reusability of code. Resuability is achieved by extending classes or implementing interfaces.

 - But this is limited to only that respective class , i.e if we want to use code in same program. If we want to use code in different programs, this is done via Packages.

 - Packages are the Java's way of grouping a variety of classes or interface together.

 - The grouping is usually done according to functionality.

 Advantages

1. Packages of other programs can be easily reused.

2. The classes in 2 different packages can have same name.

3.They provide a way to "hide" classes that are meant for internal use only.

4. Separates "Design" from "Coding".

 Defining a Package

  The package can be defined as:

                          package package_name;

  we can also declare sub-packages as:

            package package1[[.package 2][.package 3]];

  (# Note: The braces are used to separating the terms.)

This can be generally written as:

         package package1.package2.package3;

The package Hierarchy is reflected in the file system such as the package :-  package java.awt.image;  should be stored as:

 java/awt/image for UNIX system
 java\awt\image for WINDOWS
 java:awt:image for MACINTOSH file system.

 Importing Packages

The packages in java are imported by prefixing package name with import.

 such as:  import java.lang.Math;


Example:

 File name: Protection.java

 package p1;

 public class Protection 
 {
    int n=1;
    private int n_pri=2;
    protected int n_pro=3;
    public int n_pub=4;

    public Protection ( )
     { 
         System.out.println("Base Constructor");
         System.out.println("n=" + n);
         System.out.println("n_pri=" + n_pri);
         System.out.println("n_pro=" + n_pro);
         System.out.println("n_pub=" + n_pub);
     }
 }

  File name Derived.java

  package p1;

 class Derived extends Protection
  {
        Derived ( )
      {
           System.out.println("Derived Constructor");
           System.out.println(" n=" +n);

       //Protected class only
      //System.out.println(" n_pri=" +n_pri);
  
        System.out.println(" n_pro=" +n_pro);
        System.out.println(" n_pub=" +n_pub);
    }
 }

   File name: samePackage.java
   
    package p1;

   class samePackage extends Protection
    {
          samePackage () 
          {
              Protection p = new Protection ( );

              System.out.println("Same Package Constructor");
             System.out.println("n=" + p.n);

            // Protected class only
           //System.out.println("n_pri=" + p.n_pri);
             System.out.println("n_pro=" + p.n_pro);
             System.out.println("n_pub=" + p.n_pub);
           }
    }

 // To instanciate various classes in p1.

   import p1;
   public class Demo
   {
       public static void main ( String []  args )
      {
          Protection p1 = new Protection ( );
          Derived d1 = new Derived ( );
          samePackage s1 = new samePackage ( );
       }
   }

  Adding a new class to Package

  Adding a new class to existing package is simple.This can be done   as :
      
       package p1;
        class A 
          { 
              //Body of A
          }

Hiding Classes

 We can hide a class which is contained inside the package from the outside classes.

  This can be done by not declaring them PUBLIC.

 Ex-
  
    package d1;
    public class X                     // Public, Accessible outside class
       {
           //Body of X
       }
    class Y                               //Not accessible outside class
     {
         //Body of Y
     }

  since class Y is NOT PUBLIC , its object cannot be instantiated. If done so , the compiler will generate an error message.

 Ex- import d1.*;
        X object x;         // available
        Y object y;        //not available (error)

Compiling the Packages ( in cmd)

  in Command prompt :
 Ex-
  
   C:\Samples>   javac -d . Demo.java

 (# Note: Dot here represents the current folder.)
  



Comments

Popular posts from this blog

Introduction to JAVA ( Basic )