Advantages of packages in java

You have read the package in java in a recent post. In this post, we will read the advantages of packages in java.

1. Reuse of code

The most common advantages of packages in java are reusability. During the development, if you feel you are writing some duplicate code that is already written. You can create such things in the form of classes inside a package. So, whenever you need to perform that same task or need the same code then you can use it by import that package.

For example: If a company has two departments one is the HR Department,  and another is the IT Department. Both departments want to perform some calculations. So, we can place the logic of calculation in class that will be part of another package. In JAVA, we will create three classes in different packages.

package myCalculator;
public class Calculator 
{
	public int sum(int a, int b)
	{
		return a+b;
	}
	
	public int sub(int a, int b)
	{
		return a-b;
	}
	
	public int multiple(int a, int b)
	{
		return a*b;
	}
	
	public int divide(int a, int b)
	{
		return a/b;
	}
}
package hR;
import myCalculator.calculator;
public class HRDepartment {

	public static void main(String[] args) 
	{
		Calculator calculator = new Calculator();
		
		System.out.println(calculator.sum(1, 3));
		System.out.println(calculator.sub(8, 6));
		System.out.println(calculator.multiple(2, 3));
		System.out.println(calculator.divide(9, 3));
	}

}
package iT;
import myCalculator.calculator;
public class ITDepartment {

	public static void main(String[] args)
	{
		Calculator calculator = new Calculator();
		
		System.out.println(calculator.sum(1, 3));
		System.out.println(calculator.sub(8, 6));
		System.out.println(calculator.multiple(2, 3));
		System.out.println(calculator.divide(9, 3));
	}

}

In the above example, Calculator is a class in myCalculator package. So that we can use the code of class by use of import statements.

2. Categorize/Organization of project

Packages are used to organize the structure of the project that contains number of classes, interfaces, or enumerations. A project can have any number of functionality, So it would be better if you can place the related functionality classes, interfaces, or enumeration in a package. In large projects where we have several hundreds of classes, it is always better to create packages of similar types of classes in a meaningful package name. So that you can organize your project and if someone wants to check something it can quickly access. It improves the efficiency of the project.

Example: In the above example, HRDepartment and ITDepartment can have any number of classes, interfaces, or enumeration. So, it an example of a well-organized project.

3. Removes naming conflicts

The package in java plays an important role to resolve the naming conflict of classes. You must be seen you can’t create two classes with the same name(without package). But if you are creating packages then you can define two classes with the same name but in different packages. So, by use of the package, you can remove the name collision.

We can take an example from our JDK. There are two packages java.util and java.sql that contains Date class. Although the class name is the same in both packages it doesn’t create any conflict.

4. Access protection

If you don’t know about the access control you should read the access modifiers in java. The access modifiers specify access control, but the package provides an important role there. The package also provides the encapsulation in java. A package can have any classes and other sub-packages and a class can have data and code. Both are the container that holds the data.  You can read the access right within the package and outside the package from the access modifier.
By use packages with Access modifiers, Java provides access protection.

5. Modularity

A package contains several classes, interfaces, or enums. Suppose you are working on a large project having many functionalities. But all the functionalities need some common functionality(Which is base for all). You can create the common functionality in a package and use that package wherever it needs. The common package acts like a module that can use accordingly. By using the package, java supports the modularity concept.  

6. Information Hiding

By use of packages, you can hide the information of classes. You can specify the data member as private, protected, public, and default. By use of a modifier, you can specify which class data should be hidden from the other classes.

advantages of packages in java

These are the Advantages of package that are you using in daily routines.

Leave a Comment