Skip to main content

Packages in Java

Package is container of classes. It is used the class namespace compartmentalized. The package may contain class, interface or some other packages itself. It is group of related class with specific task. For example the I/O package contain Input/Output specific and applet package is applet specific.  It achieve the object oriented encapsulation task. 
Defining Package
Package are created with keyword package with name of package, it must declared first in the java source file. All classes which is declared inside the packages are classes of that package.For example:
package Employee;
Where Employee is the name package;

Importing Package;
Package once created, we can access all classes of package using keyword import.
for example; import Employee.*; the * tell the compiler to import all classes

There are two types.

  • Predefined Package : The package are inbuilt in java package for example and Java.applet etc.
  • User Defined Package: The package where created by user or programmer.
Subpackage: The package which defined in another package. For example java.applet, applet is subpackage of java. 

Package Employee
public class emp
String ename,catg;
double empid,bpay,hra,da,pf,ntp;
public emp(String n,double eid,String c,double p)

public void calc()

public void show()
System.out.println("-----EMPLOYEE DETAILS-----");
System.out.println("Employee Name : "+ename);
System.out.println("Employee Id : "+empid);
System.out.println("Employee Category : "+catg);
System.out.println("Basic Pay : "+bpay);
System.out.println("House Rent Allowance : "+hra);
System.out.println("Dearness Allowance : "+da);
System.out.println("Provident Fund : "+pf);
System.out.println("Net Pay : "+ntp);


import Employee.*;

public class emppay
public static void main(String args[])
emp e=new emp("Anu",101,"Team Leader",15000);
Packages in java

Thank You. Like Share and Subscribe 👍