Thursday, 6 September 2012

Package

PACKAGE :- Package are the container to hold  a no of raw data file of java named as Class. In other words PACKAGE are the folder on which we can store our java class without duplicity collision .

Book definition- Packages are containers for classes that are used to keep the class name
space compartmentalized. 
*For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other class named List stored elsewhere. 
*Packages are stored in a hierarchical manner and are explicitly imported into new class definitions

Need of package:-

* Java does not support multiple Inheritance like C++ directly.

* For accessing multiple inheritance in Java we need another efficient approach namely:-
   PACKAGE & INTERFACE.

*  Packages are stored in an hierarchical manner and are explicitly imported into new class definitions.

*Java provides a mechanism for partitioning the classname space into more manageable chunks

* This mechanism is the package. Thepackage is both a naming and a visibility control mechanism.

* You can define classes inside a package that are not accessible by code outside that package. 

* You can also define class members that are only exposed to other members of the same package.
This allows your classes to have intimate knowledge of each other, but not expose
that knowledge to the rest of the world.
_________________________________________________________________________________

In this note we have a keen look into the topics :-

1) Defining a Package
2) Creating a Package
3) Accessing a Package
4) Understanding the CLASS PATH
5) Importing the Package

________________________________________________________________________________

1) Defining a Package :-

* To create a package is quite easy: simply include a package command as the first statement in a Java source file. 
* Any classes declared within that file will belong to the specified package. 
*The package statement defines a name space in which classes are stored. 
*If you omit the package statement, the class names are put into the default package, which has no name. (This is why you haven’t had to worry about packages
before now.)
* While the default package is fine for short, sample programs, it is
inadequate for real applications. Most of the time, you will define a package for
your code.

This is the general form of the package statement:
package pkg;

or we can creat our own package of desired name followed by name " package "
ex-
package mypack1;
_________________________________________________________________________________

2) Creating a Package:-

FOR COMMAND MODE COMPILATION in jdk  & creating class using NOTEPAD
* For creating a Package .... we can directly create a Folder under desired PARENT folder with the name       what we want for the PACKAGE.
* We can store our desired class  & methods inside the folder directly after writing the code in TEXT EDITOR / NOTEPAD.
* We need to compile our package class java files before we import it any MAIN CLASS of a PROGRAM.
FOR JAVA-IDE like- NETBEAN, ECLIPSE, jBoSS compiler 

* when we create a new file / PROJECT in any of above compiler first it ask us for the PACKAGE.
*At that time we can mention our desired new Package name else we can merge our file into EXISTING package.
* After creation of package name only -> it ask for the Class name .
* We can Specify the class name before creation else for several new class we can create Dynamically in the specified Directory or Package Folder.

example / Demonstration :-




_________________________________________________________________________________


3) Accessing a Package:-

* A package can be accessed using the key word " import "  followed by the package name.
* We can access a Particular class java file or for multiple file access we can use " * " symbol.
* while we use the " * " Symbol, it access the whole package at a instance
 But in this case compilation takes a little more time than normal compilation.
 example :-
 import pck1.mood;   // it can only access the class mood & its function from package pck1

import pck1.*;   // as it import whole package, it can be accessed all class, sub-class & methods inside  "pck1"

But the type declaration of class & Methods matter, for accessing them from package

* We can protect the unauthorized Accessing of class & method from out side world. 
* As we have different Access Specifier like PRIVATE, PROTECTED, PUBLIC & DEFAULT (NO-mODIFIER)
With different Access Specifier how can we access to whom .....  can be confirmed from the table given-

_________________________________________________________________________________

4) Understanding the CLASS PATH :-

* The path on which we store the package & class , we have to mention it during the Import
* If we have the package under the parent folder / path on which we store our necessary class , then we can 
 import it as usual in normal access :- 
import pck1.*;

but some time the compiler can not access the necessary file without Complation.
* some time we have need different or Multiple import of Package.
* At that time also we need the real time compilation of the classes / methods.
* if suppose we have to save a class"smile"  in " pck1" package 
   & we have main class "mood" which access the package "pck1" in parent folder- "chinny" in " H " drive 
then the setting the class path is done in the way expressed in the pic given below:-


------------------------------------------------------------------------------------------------------------


5) Importing the Package :-

*  we can import the package and/or packages in a main class to access other class & function.
* we can use " import " keyword for importing / including the classes in to main class
* either we can import a single class or the whole class by using " * " symbol
* We can import more than one package with a single statement which call one package to other & to  another
Example :-
 import pck1[.pck2[.pck3]] . *;

with this kind of statement main class import " pck1".
* But "pck1" itself access the package "pck2"
* again "pck2" access the other package "pck 3"
* So with this kind of dynamic package importing , we can access the other class & method of Another package which can be easily accessible by a single main package Import.

In general we may import more than 1 package by  multiple import statement 
 Example:-

import pck1.*;
import pck2.*;
import pck3.*;
.
.
.
etc.......
 a sample program example is given below for how a package is imported:-