Friday, March 31, 2017

Packages in Java with Examples

In this post we will talk about Java packages with examples. This is one of the core concept in Java. A package is represents a directory that contains related group of classes and interfaces. We can also define a package is a mechanism to encapsulate group of classes,interfaces and Sub packages. It is easy to organize class files into packages.Packages can help programmer to avoid name conflicts and to control access of classes,interfaces and enumeration etc..Therefore by using packages we can prevent naming conflicts.

Advantages of package:


  • packages are useful to arrange related classes and interfaces into a group.This makes all their classes and interfaces performing the same task to put together in the same package. For example,in Java,all the classes and interfaces which perform input and output operations are stored in java.io package 
  • packages hide the classes and interfaces in a separate sub directory,so that accidental deletion of classes and interfaces will not take place
Types of packages:

There are two different types of packages in Java. They are

  1. Built-in package
  2. User-defined package
Built-in package:

Built-in packages means which are already available in Java language. These packages provides all most all necessary classes,interfaces and methods for the programmer to perform any task in his programs. This makes the programming easy.

Examples:
java.lang package
java.util package
java.io package
java.awt package
java.net package
java.applet package
java.sql package

User-defined package:
The users of the java language can also create their own packages. They are also called user-defined packages. User-defined packages can also be imported into other classes and used exactly in the same way as the Built-in packages.

Let us see how to create a package of our own and use it in any other classes as shown below.

The general form of the package statement:

package pkg;

Here pkg means name of the package

Example: 
package myPackage;

package pkg.subpackagename;//to create a sub package within a package

Let us understand how to create a package with example:

package pack;
public class sum
{
private int a,b;
public sum(int x,int y)
{
a=x;
b=y;
}
//method to find sum of two numbers
{
public void sum()
{
System.out.println("sum="+(a+b));

}
}

output:
c:\>javac -d . sum.java
c:\>

Here -d option tells the java compiler to create a separate sub directory and place the .class file there. The dot(.)after -d indicates that package should be created in the current directory i.e. C:\>

That means our package with sum class is ready. Now to create an object to sum class we can write as:
pack.sum obj=new pack.sum(13,23);

class packDemo
{
public static void main(String args[])
{
//create sum class object
pack.sum obj=new pack.sum(13,23);
//call the sum method
obj.sum();
}
}

Output:
c:\> javac packDemo.java
c:\> java packDemo
sum=36

Every time we refer to a class of a package,we should write the package name before the class name as pack.sum in the above program. This is not convenient for the programmer. To overcome this,we can use import statement only once in the beginning of the program as:

import pack.sum;

Once import statement is written as shown earlier,we need not use the package name before class name in the rest of the program and we can create the object to the sum class,in normal way as:

sum obj=new sum(13,23);

Example:
import pack.sum;
class packDemo
{
public static void main(String args[])
{
//create sum class object
sum obj=new sum(13,23);
//call the sum method
obj.sum();
}
}

Output:
C:\> javac packDemo.java
C:\java packDemo
sum=36

Note:

  • If we are not taking any package statement then the current working directory Acts as default package
  • The first non-comment statement in any java source file is package statement.
How to create sub package:

we can create a sub package in a package in the format:

package pack1.pack2;

That means we are creating pack2 inside pack1. To use the classes and interfaces of pack2,we can write import statement as:

import pack1.pack2;

This concept can be extended to create several sub packages.

Example:

package learnprograming.bylucky;
public class simple
{
public void display()
{
System.out.println("welcome to learnprogramingbyluckysir");
}
}
Output:
c:\>javac -d . sample.java
c:\>

When we compiled above program,the compiler creates a sub directory with the name learnprograming. Inside this,there would be another sub directory with the name bylucky is created.In this bylucky directory,sample class is stored.Suppose user wants to use sample class of learnprograming.byluucky package,he can write a statement as:

import learnprograming.bylucky.sample;

Example:

import learnprograming.bylucky.sample;
class packDemo
{
public static void main(String args[])
{
//create object to sample class
sample s=new sample();
//call the display() method
s.display();
}
}
Output:
c:\>javac packDemo.java
c:\>java packDemo
welcome to learnprogramingbyluckysir

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...