Saturday 23 March 2013

JAVA PACKAGES


Package is a collection of related classes.  Each class defines number of methods.  Java packages are classified into 2 types.
1.      Java API (Application Programming Interface) packages (or) Predefined packages (or) Built-in packages: These packages are defined by the system.  Some of the example for system defined packages are java.lang, java.util, java.io, java.awt, java.applet etc.,
2.      User defined packages: These packages are defined by the user.

Defining a Package:
            To define a package, place “package” command as the first statement in the Java source file.  So, that any class declared within that file will belong to the specified package.  The syntax of package creation is as follows.
            Syntax:            package pack-name;
where pack-name is the name of the package.
            Example:         package mypack;
                                    public class number
                                    {
                                                public void add (int a,int b)
                                                {
                                                System.out.println(“Sum=”+(a+b));
                                                }
                                    }

Ø  The class that is defined in the package must be start with the public access modifier.  So, that it can be accessible by any another of them.  If it is not public, it is possible to access only in that package.
Ø  Java uses file system directories to store packages.  We save the program with number.java and compile the package is as
javac –d . number.java
Due to this compilation mypack directory is automatically created and .class file is stored in that directory.
Ø  Package creation has completed.  The package information is now including in our actual program by means of “import” statement.  “import” is a keyword that links the package with our program.  It is placed before the class definitions.
                              import mypack.*;
                                          or
                              import mypack.number;

Example 1:

import mypack.number;
class pack
{
            public static void main(String[] args)
            {
                        number obj=new number();
                        obj.add(3,4);
            }
}

Example 2:

package math1;
public class Mcal
{
            public void square(int x)
            {
            System.out.println("Square of "+x+" is"+(x*x));
            }
            public void cube(int x)
            {
            System.out.println("cube "+x+" is"+(x*x*x));
            }
}


import math1.Mcal;
class calc
{
            public static void main(String[] args)
            {
                        Mcal obj=new Mcal();
                        obj.square(3);
                        obj.cube(4);
            }
}

Example 3:

package p6;
public class Balance
{
String name;
double bal;
public Balance(String n, double b)
{
name=n;
bal=b;
}
public void show()
{
if(bal<0)
{
System.out.println(name+"---"+bal);
}
}
}


import p6.Balance;
class Account
{
            public static void main(String[] args)
            {
            Balance c[]=new Balance[3];
            c[0]=new Balance("John",123.33);
            c[1]=new Balance("Arun",157.02);
            c[2]=new Balance("Siri",-12.33);
            for(int i=0;i<3;i++)
            c[i].show();
            }
}


Note: 1.           Java source file contains only one public class and any number of non-pubic classes.  The filename should be same as the name of the public class with .java extension.
            2.         Package contain any number of public and default classes.
                        Example:         package p1;
                                                public class x
                                                {
                                                            // body of x                 this class is accessible
                                                }
                                                class y
                                                {
                                                            // body of y                 not accessible due to non-use
                                                }                                              of public access modifier
                        Let we want to access all the classes of information then the classes are stored in different files with the same package name.

            Example:         package mypack;
                                    public class number
                                    {
                                                public void add (int a,int b)
                                                {
                                                System.out.println(“Sum=”+(a+b));
                                                }
                                    }

                                    package mypack;
                                    public class number1
                                    {
                                                public void sub(int a,int b)
                                                {
                                                System.out.println("sub="+(a-b));
                                                }
                                    }

                                    import mypack.number;
                                    import mypack.number1;
class pack
{
            public static void main(String[] args)
            {
                        number obj=new number();
                        obj.add(3,4);
                        number1 obj1=new number1();
                        obj1.sub(6,2);
            }
}

No comments:

Post a Comment