Java Packages

It is repeatedly said that one of the main features of the Object Oriented Programming is the ability to reuse the code already written by the programmer. One way of achieving is by extending class and using the interface. It has a limitation. What will you have to do if you have to use a class from another program without physically copying them into the program at the time of development? So another way of achieving the concept of reusability in Java is via the use of Packages. In this chapter you will learn about how packages are used within a Java program.

What are packages?

Packages in Java are groups of similar types of classes’ interface and sub packages. It is a way of grouping a variety of classes or interfaces collectively. The grouping is usually done according to functionality. The Java packages act as containers for Java classes.
There is also a term named sub-packages. Package inside the package is called the sub-package. It should be created to categorize the package further. Here’s are the benefits of organizing classes:
  1. The classes contained in the packages of another program can be easily reused.
  2. Packages also allow programmers to separate design from coding.
  3. In packages, classes can be declared uniquely compared with classes in other packages.
  4. Java Packages provide a way to ‘hide’ classes thus preventing other programs or packages from accessing classes that are meant for internal use only.
  5. Packages provide access protection.
  6. Java package removes naming collision.
For most applications, programmers need to use different sets of classes, one for the internal representation of program’s data and other for the external presentation purposes. You may build your own classes for handling your program data and use existing class libraries for designing class libraries for designing user interface.

Creating Packages

While creating a package, programmers must choose a name for the package and include a package statement along with that name at the top of the source program that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package. There can be only one statement of package in each source code, and it applies to all types in the file.

To compile the Java programs with package programmers have to do use-d option as shown below

javac -d Destination_folder file_name.java
After that, a folder is created with the given package name in a specific location and the compiled class files will be placed in that folder. The general form of creating package is:
package pack_name;
public class firstClass
{
. . . . . . . . //body of the class.
. . . . . . . .
}
Here, the package name is pack_name and the class firstClass is considered a part of this package. This listing would be saved in as a file called firstClass.java and will be stored in the directory named pack_name. When the file will get compiled, Java will create a .class file and store it in the same directory.

Example of Java Program implementing Package

Example:
package weapons;
interface ammo {
public void sniper();
public void rifle();
}

Now we have to implement the above interface in the same package weapons like this

Example:
package weapons;
/* File name : arms.java */
public class arms implements Ammo{
public void sniper(){
   System.out.println("Magnum Sniper");
 }
  public void rifle(){
  System.out.println("Sub_Machinegun");
 }
  public int noOfBullets(){
 return 0;
}
public static void main(String args[]){
  arms m = new arms();
  m.sniper();
  m.rifle();
 }
}
Now compile the java files as shown below
$ javac -d . ammo.java 
$ javac -d . arms.java

Accessing Package

There are three ways to access the package from outside the package.
  • import package.*;
  • import package.classname;
  • fully qualified name.
You can use the import statement when there are many references to a particular package. The same approach can be used to access the user defined packages as well. The import statement can be used to search a list of packages for a particular class. The general form of import statement for searching a class is:
import package1 [.package2] [.package3].classname;

Here, package1 is the name of the top-level package. package2 is the name of the package that is inside the 1st package i.e. package1 and so on. Programmers can use any number of packages in the package hierarchy. At the end, the explicitly class name is specified.

1 comment:

  1. DATA STRUCTURE, CORE JAVA, JAVA TUTORIAL, JAVA STUDY, JAVA TECH, JAVA PROJECTS, JAVA ASSIGNMENTS, NIIT PROJECTS, NIIT ASSIGNMENMENTS, NIIT CYCLE TESTS, NIIT LAB@HOME, NIIT EXAMS MT, NIIT PMR, NIIT NOTES AND MUCH MORE GO TO https://solutionrk.blogspot.in/

    ReplyDelete