We know that the concept reusability is one of the advantages of inheritance and interface. This is done by extending classes and implementing interface. But this is limited for reusing the classes in a program. If we want to use classes from other program without copying physically in a program, then java provides a concept that is package which accomplish this task

In general a package is nothing but a container which contains a variety of classes and interfaces. In other word we can say a package represent a directory which contains classes and interfaces.

For example see the following statements

import java. lang.*;
Here java. lang is a package that we are importing .
Here java is a directory name and lang is sub-directory within it and * represent all the classes and interfaces of the lang sub-directory.
Benefits
A package contains classes which can be easily reused in other programs.
Packages are used to make a group by taking related classes and interfaces.
Package hides the classes and interfaces in a separate sub-directory.We can say a package is like a library where classes and interface are like books in that library and can be reused in several times. This reusability is the big advantage in object oriented programming
Package provides access protection.
Java package removes naming collision.

Types of packages
In java there are two types of packages, such as:

In built packages (java API packages)
User defined package
Inbuilt package
There are some packages already available in java of its own. These package contains classes , interfaces and methods to perform any task in a program . This package is also known as java API package. Here I have presented some in built packages of java which are as follows :
Java. lang: Lang stands for language. This package contains classes and interface for primitives, string, mathematical functions, threads and exceptions.

Java. util: Util stands for utility. This package includes classes and inheritance like vector, arrays, hash tables, stack, data etc.

Java. io: io stands for input and output. This package contains input/output classes which are used for input and output of data.

Java. awt: awt stands for abstract window toolkit. It contains a set of classes and interfaces for developing GUI (Graphical user interface) and also include classes for windows, buttons, list, menus etc.

Java.net: net stands for network. This package is used for client server programming. Classes including in this package are using for network, creating sockets, and at client and server to establish communication between them.

Java. applet: classes containing in this package which are used for creating and implementing applets.
User defined packages
When the user is defining the packages called as user defined packages.User defined packages are also imported into other classes.

The general form to create a package is

package package name; //To create a package
package package name. Sub package name; //to create a sub spackage within a package
Here the keyword package is used for creating a package.
When we defining a class then we will normally define under a package. For example

package demo;
class Race ;
{
- - - - - - // Body of class
- - - - - - -- -
}
Here the package name is demo and the class Race is consider as a part of this package.
Let us see the following program

package p1;
class Silan
{
public static void main(String args[])
{
System.out.println("JavaRace");
}
}
output
D:\>javac -d d:\Silan.java
D:\>java p1.Silan
JavaRace

Compilation
Compiler does not create package physically in current working directory just with javac command. Packaged classes must be compiled with -d option to create package physically.
For example, javac -d.Silan.java
With this command, compiler creates package p1 with Silan class and place it in current working directory. The functionality of -d is creating package with the name mentioned in java file and moving .class file into that package and finally storing that package in the given path.
With the command Javac -d d:\text Silan.java, compiler creates package p1 with Silan.java in d:\text folder. Text folder must be existed in d drive before this program compilation.Packaged class code changed by compiler
After compilation, compiler replaces class name and its constructor name with its package name.class name. it is called fully qualified name.

Silan.java

Example
package p1;
class Silan
{
public static void main(String args[])
{
System.out.println("JavaRace");
}
}
Silan.class

Example
class p1.Silan extends java.lang.object
{
p1.Silan()
{
super();
}
public static void main(String args[])
{
System.out.println("JavaRace");
}
}
Execution
We must use package name in executing a packaged class else it leads to Exception.So the command for execution is : java p1.Silan

Output
Example
package demo;
public class Race
{
public void show()
{
System.out.println ("Monalisa Sahu");
}
}
Class Test
{
public static void main (String args[ ])
{
demo.Race ob= new Race();
l. show( );
}
}
Let's see another program:
Output
In the above statement it is convenient that we can write the package name before the class name by using import statement such as:

Example
import demo.Race
Then the code will be
import demo.Race;
class Test
{
public static void main (String args[])
{
Race l= new Race( );
l.show( );
}
}
Output
NOTE: The import statement is used to access a package . The general form of import statement for accessing a class is
import Packagename .classname ;
Now the sourse file wii be saved as vivtech.java and then compiled . The output of compilation is vivtech.class file. Now the source file and compile file wiil be saved in the current java directory of which Pranaya was a sub directory then we run the program and get the output.

Let us see another program for better understanding the concept of package

Example
package viswass;
public class Vivtech
{
private int x, y;
public Vivtech (int p, int q)
{
x=p;
y=q;
}
public void add ( )
{
System.out.println("addition of x and y is " + (x+y));
}
}
import viswass.Vivtech;
class Race
{
public static void main(string args[ ])
{
Vivtech v=new Vivtech (5, 20);
v.add();
}
}
Output
Now let us see to add another number class silan to the same package viswass

Example
package viswas;
public class silan
{
private int a,b;
public Silan (int c, int d)
{
a=c;
b=d;
}
public void mul ( )
}
system.out.println("the multiplication of a and b is " + (a*b));
import viswass.Vivtech;
import viswass.Silan;
class Test
{
public static void main ( String args[ ])
{
Vivtech v= new Vivtech (5, 20);
Silan s = new Silan (4, 6); v.add( );
s.mul( );
}
}
Output
Here we are using multiple import statement such as:
import viswass.Silan;
Instead of writing multiple import statement we can write a single import statement as:
import viswass.*;
Where * represents all the classes of the particular package viswass.

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment