Monday, 30 January 2017

Packages

Package
Packages are just like class libraries in other languages. With the help packages we can use classes from other programs without physically copying then into the current program we are developing. Java groups various classes and interfaces into packages. Two classes into two different packages can have same name, to refer these classes we use name of package with class name. Some classes are for internal use of the package, package can hide such classes from other packages to use.

Java API Classes
Java provides various classes grouped into various packages as per their functionality.  Some of the java packages are:
Java. Lang:  This package contains language support classes. It contains the classes for math functions, threads, strings and exception etc.
Java Util: This package contains language utility classes like hash table, date, random numbers etc.
Java.io: This package contains input output support classes.
Java.awt: This package contains graphical user interface related classes. It contains classes such as windows, button, list, menu etc.
Java.net: This package contains classes related to networking. It contains classes for communicating computers on local network as well as internet.
There are two ways to access classes from packages. First by using full qualifying class name of classes. This is done by using a package name of the class, dot operator and class name.
Example: java.awt.color

awt is a package in java package.  To use classes in a program we will need to import them.
Example:        
import java.awt.Color;

Second method is by importing whole package. Example:  
import java.awt.*;
Naming Convention
Name of the package starts with small case latter whereas name of the class starts with the Upper case latter. Example:
double y = java.lang.Math.sqrt(5);
In this statement lang is the name of package, Math is the name of class and sqrt() is method in class Math. Name of the method starts with lower case.
Creating a package
To create a package first we will write a keyword package then name of package. This should be the first statement of the java source file. Then we can define class as we have seen in previous tutorials. Example:
            package myPackage;
public class myClass
{
---------------------
}
Here myPackage is the name of package and myClass is the name of class which is part of myPackage. Above listing will be saved as myClass.java in a folder myPackage.
We can create a package within a package. Example:
            package myPackage.secondPackage;
Similarly directories also created with a directory with same name of package and sub package. In this example a directory secondPackage will be created in a directory myPackage like (myPackage/secondPackage).
Using a Package
Create a new java project in eclipse with project name myProject, add new class to src folder of the project with name MyClassOne and package name myPackage. Java file MyClassOne.java will be created. Add showClass() method in this java file as given below.  
Example:
package myPackage;

public class MyClassOne {
       public void showClass()
       {
              System.out.println("Class myClassOne");
       }

}

Create a new java class with name PackageExample in the src folder of same project.
Example:
import myPackage.myClassOne;

public class PackageExample {
       public static void main(String args[])
       {
              myClassOne myClassObject = new myClassOne();
              myClassObject.showClass();
       }

}
The output of this program is: Class myClassOne

In this example we have imported class MyClassOne in this class with statement import myPackage.myClassOne. In method main we created object of this class myClassObject and called method myClassObject.showClass().

Importing class from other package
Create a class named MyClassTwo and package name myPackageTwo in the same project myProject. Create a method with name showClassTwo() as shown below.

Example:
package myPackageTwo;

public class MyClassTwo {
       protected int x = 20;
       public void showClassTwo(){
              System.out.println("Class myClassTwo");
              System.out.println(" x = " + x);
       }

}

Create a new class with name PackageExampleTwo in the src folder of myProject and add code in the class as given in below example.
Example:
import myPackage.MyClassOne;
import myPackageTwo.MyClassTwo;
public class PackageExampleTwo {
       public static void main(String args[])
       {
                     MyClassOne myClassObject = new MyClassOne();
                     MyClassTwo myClassObject2 = new MyClassTwo();
      
                     myClassObject.showClass();
                     myClassObject2.showClassTwo();
       }
}
The output of this program is:
                                                Class myClassOne
Class myClassTwo
x = 20 
Import class MyClassOne from package myPackage import myPackage.myClassOne and also import class MyClassTwo from myPackageTwo import myPackageTwo.MyClassTwo. Create objects of both the classes and call methods of both the classes.  

Extending Imported Classes
Crtaet a new class with name PackageExample3 in src folder of project myProject. Import class myClassTwo import myPackageTwo.MyClassTwo and create a class myClassThree and extend it with class MyClassTwom, class MyClassThree extends MyClassTwo. Create objects of class MyClassTwo and MyClassThree, call methods of both classes as given in below example.

Example:
import myPackageTwo.MyClassTwo;

class MyClassThree extends MyClassTwo
{
       int y = 50;
       void showThree(){
              System.out.println("Class myClassThree");
              System.out.println(" x = " + x);
              System.out.println(" y = " + y);
             
       }
      
}

public class PackageExample3 {
      
       public static void main(String args[])
       {
                     MyClassTwo myClassObject2 = new MyClassTwo();
                     MyClassThree myClassObject3 = new MyClassThree();
                     myClassObject2.showClassTwo();
                     myClassObject3.showThree();
       }
}
The output of this program is:
       Class myClassTwo
       x = 20
Class myClassThree
       x = 20
       y = 50

Hidden Classes
Sometimes we create internal classes and hide them fom outside access, these classes are called hidden classes. Hidden classes are declared as not public, keyword public is not used with such classes.

Example:

package myPackage;

public class MyClassOne {
       public void showClass()
       {
              System.out.println("Class myClassOne");
       }
      
       class HiddenClass{
              public void showClassTwo(){
                     System.out.println("Hidden Class");
              }
             
       }

}

In this example class HiddenClass is not public, keyword public is not used to declare this class  so it is hidden and cannot be accessed outside of this package.



No comments: