Packages in Java

packages in java

 

Hello friends, welcome to programming scoop. Today we will understand a very interesting and important topic in java that is packages.so let's start this tutorial.

What is Package ?

 
Packages are the way of organizing files into different directories according to their functionality,usability as well as category they should belong to.

Basically packages are used to create collection of classes and interfaces so that the we can use their methods in other program as per need so  we don't have to write the same code again and again which is already present in existing class.

One of the important object oriented feature is the reusability of code.This feature can be achieved by extending a class or implementing an interface in the program.If one wants to reuse the code in some other program without actually physically copying it, then the solution is to use the concept of package. The concept is similar to class libraries. The use of packages is another way of achieving reusability in Java.

A package in Java is used to group related classes. Think of it as a folder in a file directory.

Features of package are:


  • The classes in the package of other program can be used easily reused.
  • It is a convenient way to store together groups of related classes. It makes easy to locate and use the required class.
  • Java supports too many packages and each having to perform different functionality.

Types of packages-Packages are divided into two categories:

  • Built-in Packages (packages from the Java API)
  • User-defined Packages (create your own packages)


Built-in Packages:-
Java provides no of predefined packages which contain no of classes that we use in our program. Some of the most common packages are :-

  • java.awt - contains classes related to GUI
  • java.net - contains classes related to networking
  • java.io - contains classes related to input/output

The complete list of packages can be found at Oracles website: https://docs.oracle.com/javase/8/docs/api/.


User Defined Packages:-

Java allows us to create our own packages to organize his related classes. Such packages are called user-defined packages.

Now we will learn how to create packages and how to import them in our program for using them.

How to create package in Java?

Creating of package:-

For creating a package first we have to include package statement in our program:-

    syntax:- package packagename;
   
for eg:-

  1.     package world;   //creating package
  2.     public class HelloWorld
  3.     {
  4.         public static void main(String []args)
  5.         {
  6.             System.out.println("hello World");
  7.         }
  8.     }

Now save this program as HelloWorld.java. Here we have to save the file name with the class name because HelloWorld class is public if we don't have public class in our program then we can use any name to save our program.

Now after saving the program we have to compile the program

compilation:-

To create a package we have to compile our program by using directory statement(-d).



  • Syntax :- javac -d . filename.java
  • for eg  :- javac -d . HelloWorld.java

now we have created our package.

 
package created
   creating package


now we will run the program.

running of program:- 


To run a program we have to use package name follwed by .class file as shown below:-
  • syntax :-  java packagename.mainclassname.class
  • for eg  :-      java world.Helloworld.class


Now we have created our user defined  package and able to run it.

How to import package?

 
Now we will learn how to import packages in our program. It is a very simple process For importing a package we only have to use import statement in our program as shown below:

  • syntax :- import packagename.classname; 
  • for eg  :- import java.util.Scanner;
this will include all the method of Scanner class in our program so that we can use;


Now let's take one example to understand the whole process:-

In this example we will create two program in first program we will create package and in other we will import package to use the method of first program.

Creating First program:-

In this program we will a class which contains a method which calculate the sum of two no. and print on screen and we will create  a package which store this  class.


  1. package mypackage;
  2. public class addition
  3. {
  4.     public void sum(int a,int b)
  5.     {
  6.         System.out.println("Addition = "+(a+b));
  7.     }
  8. }

Creating Second program:-

In this program we will import addition class of mypackage that we created in first program and use that class for calculating sum of two no.


  1. import mypackage.addition;
  2. class main
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         addition obj=new addition();
  7.         int a=10;
  8.         int b=20;
  9.         
  10.         System.out.println("Sum of "+a+" and "+b);
  11.         obj.sum(a,b);
  12.         
  13.     }
  14. }
Output
output


Now I hope you understand what package is and how to create and import package. Share this post to other so they can also understand what is package in java? 
If you still have any doubt or any query please feel free to ask in comment section.
Visit Latest posts:- programmingscoop