Packages and Subpackages in Java

Introduction to Packages and Sub Packages in Java

A package in Java is a namespace that organizes related classes and interfaces. Packages are used to prevent name collisions and to make code more organized and reusable.

Java comes with a number of built-in packages, such as java, lang, and io. These packages contain classes and interfaces that are essential for writing Java programs.

In addition to the built-in packages, you can also create your own custom packages. To create a custom package, you simply need to create a directory structure for the package and then place your class and interface files in the directory structure.

Once you have created a package, you can import it into your code using the import statement. For example, the following code imports the java.lang package:


  import java.lang.*;
 

This import statement allows you to use the classes and interfaces in the java.lang package without having to specify the package name each time.

Subpackages are packages that are nested within other packages. For example, the java.util package is a subpackage of the java package. Subpackages are useful for organizing related classes and interfaces into smaller groups.

To create a subpackage, you simply need to create a directory within an existing package. For example, to create a subpackage called myutils within the java.util package, you would create the following directory structure:


  java/util/myutils
 

You can then place your class and interface files in the myutils directory.

To import a subpackage, you need to use the import statement and specify the full package name of the subpackage. For example, the following code imports the java.util.myutils subpackage:


  import java.util.myutils.*;
 

This import statement allows you to use the classes and interfaces in the java.util.myutils subpackage without having to specify the full package name each time.

Packages and subpackages are a powerful feature of Java that can help you to write more organized and reusable code.

Benefits of using packages and subpackages
  • Avoid name collisions:Packages and subpackages help to prevent name collisions by providing a namespace for your classes and interfaces. This means that you can have two classes with the same name in different packages without any problems.
  • Organize your code:Packages and subpackages can be used to organize your code into logical groups. This can make your code easier to read, maintain, and reuse.
  • Reuse code:Packages and subpackages can be used to reuse code. For example, if you have a class that implements a common function, you can put that class in a package and then import the package into your code whenever you need to use the function.

The following Exercises will be carried out

  • Creating a package using Eclipse
  • Adding a class file to the package
  • Importing a class file from within a package
  • Importing multiple classes from within a package
  • Creating a subpackage
  • Adding a class file within a subpackage
  • Importing the class file from within a subpackage
  • Resolving import collides with another import statement error

Step 1 : Right Click on the project and select a new package as shown in the image below
Creating a package in eclipse
Step 2 : Give a name for the package. Here we are creating a package by name calculatorPackage
Creating a package in eclipse
Step 3 : Adding a new class file to the package.
Creating a class inside a package in eclipse
Step 4 : Enter the class name. Here we are adding the class Addition to the calculator package
Creating a class inside a package in eclipse
Step 5 : Addition class is created inside the calculator package as shown in the image below
Creating a class inside a package in eclipse
Step 6 : Lets add a method by name add(int,int) in the Addition class.
Creating a class inside a package in eclipse
Code for the Addition class is given below

package calculatorPackage;
public class Addition {
	public int add(int a, int b)
	{
		return a+b;
	}
} 
Step 7 : Lets add another class file by name Subtraction to the calculator package
Creating a class inside a package in eclipse
Step 8 : Give the class name as subtraction as shown in the image below
Creating a class inside a package in eclipse
Step 9 : Subtraction class is created inside the calculator package as shown in the image below
Creating a class inside a package in eclipse
Step 10 : Lets add a method by name subtract(int,int) in the Subtraction class.
Creating a class inside a package in eclipse
Code for the Subtraction class is given below

    package calculatorPackage;
    public class Subtraction {
      public int subtract(int a,int b)
      {
        return a-b;
      }
    }  
Step 11 : Let's import the Addition class inside another package and create an object for the Addition class. We will also invoke the add method inside the addition class to add two numbers and display the result.
importing a package in another package
Code for the importing the Addition class from the calculator package in another package is given below

    package bbsProject;
    import calculatorPackage.Addition;
    public class bbsdemo {
        public static void main(String args[])
        {
           Addition a = new Addition();
           int result = a.add(3, 4);
           System.out.println(result);
        }	
    } 
Step 12 : Let's import the Subtraction class inside another package and create an object for the Subtraction class. We will also invoke the subtract method inside the subtraction class to subtract two numbers and display the result.
importing a package in another package
Code for the importing the Subtraction class from the calculator package in another package is given below

package bbsProject;
import calculatorPackage.Subtraction;
public class bbsdemo {
    public static void main(String args[])
    {
      Subtraction s = new Subtraction();
       int result = s.subtract(4, 2);
       System.out.println(result);
    }	
}    
Step 13 : Let's import all the classes from the calculator package inside another package and create an object for the Addition and Subtraction classes. We will also invoke the subtract and add methods and display result.
importing a package in another package
Code for the importing all the classes from the calculator package in another package is given below

    package bbsProject;
    //importing all classes from calculator package
    import calculatorPackage.*;
    public class bbsdemo {
        public static void main(String args[])
        {
           Subtraction s = new Subtraction();
           int result = s.subtract(4, 2);
           System.out.println(result);
           Addition a = new Addition();
           result = a.add(3, 4);
           System.out.println(result);
        }	
    }      
Step 14 : Let's create a subpackage inside the calculatorPackage
creating a subpackage inside another package
Step 15 : Enter the subpackage name as multiplierpackage as shown in the image below. The subpackage name should follow the package name as packagename.subpackagename
creating a subpackage inside another package
Step 16 : Create a Multiplication class inside the multiplierpackage which is a subpackage
creating a class inside the subpackage creating a class inside the subpackage
Step 17 : Adding the multiply method inside the Multiplication class within the subpackage
creating a class inside the subpackage
Code for the Multiplication class added within the subpackage
  
    package calculatorPackage.multiplierPackage;
    public class Multiplication {
      public int multiply(int a,int b)
      {
        return a*b;
      }
    }     
Step 18 :Import the Multiplication class within the subpackage in another package
importing a class from inside the subpackage
Code for the importing Multiplication class from within the subpackage into another package

    package bbsProject;
    import calculatorPackage.multiplierPackage.Multiplication;
    public class bbsdemo {
        public static void main(String args[])
        {
             Multiplication m = new Multiplication();
             int result = m.multiply(3, 2);
             System.out.println(result);
           
        }	
    } 
Step 19 : Understanding import colliding with another import statement - This error happens when we import two classes with the same name from two different packages.
Let's create another package with the name calculatordummy and add the Addition class inside the calculatordummy package
Create the Package by name calculatordummy
creating a class inside the subpackage
Add the addition class to the calculatordummy package
creating a class inside the subpackage
Add the add method to the Addition class
creating a class inside the subpackage
Now importing Addition class from CalculatorPackage and CalculatorDummy package in the main file. This leads to collides error as shown below in the image
import colliding with another import statement
Resolving import collides with another import statment error – for the Addition class from the calculator dummy package we specify the classname as packagename.classname to avoid collision with another Addition class from calculator package as shown in the image below
import colliding with another import statement
import collides with another import is resolved by giving the full name for the class as packagename.classname to avoid the collision.

    package bbsProject;
    import calculatorPackage.Addition;
    public class bbsdemo {
        public static void main(String args[])
        {
           Addition a = new Addition();
           int result = a.add(3, 5);
           System.out.println("Addition from calculatorPackage "+result);
           calculatordummy.Addition b = new calculatordummy.Addition();
           int result1 = b.add(4, 5);
           System.out.println("Addition from calculatordummy "+result1);
        }	
    }      

Code for importing two classes with the same name from two different packages. This is a demo on how to prevent collison on import error.


  package javaWinterSemester;
  import vitUniversity.faculty;
  import vitUniversity.scope.lab;
  public class javademo {
  
    public static void main(String[] args) 
    {
      faculty satish = new faculty();
      satish.display_faculty();
      lab s = new lab();
      s.display_rules();
      vitUniversity.sense.lab k = new vitUniversity.sense.lab();
      k.display_rules();
    }
  }
  
  package vitUniversity;
  public class faculty {
    
    public void display_faculty() {
      System.out.println("hi from scope faculty");
    }
  }
  
  package vitUniversity.scope;
  public class lab {
    public void display_rules() {
      System.out.println("rules from scope lab class");
      
    }
  }
  
  package vitUniversity.sense;
  public class lab {
    
    public void display_rules() {
      System.out.println("rules for sense lab class");
      
    }
  }