What is package? Explain types of packages and its uses.
Packages
- Java package is a mechanism of grouping similar type of classes, interfaces, and sub-classes collectively based on functionality. When software is written in the Java programming language, it can be composed of hundreds or even thousands of individual classes. It makes sense to keep things organized by placing related classes and interfaces into packages.
- A java package is a group of similar types of classes, interfaces, and sub-packages.
- Package in java can be categorized in two forms, built-in package, and user-defined package.
- There are many built-in packages such as java, lang, awt, java, swing, net, io, util, SQL, etc.
- Here, we will have the detailed learning of creating and using user-defined packages.
- Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations, and annotations easier, etc.
- A Package can be defined as a grouping of related types(classes, interfaces, enumerations annotations ) providing access protection and namespace management.
- Some of the existing packages in Java are:
java.lang=bundles the fundamental classes
java.io=classes for input, output functions are bundled in this package
Programmers can define their own packages to bundle groups of classes/interfaces, etc.
- It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, annotations are related. Since the package creates a new namespace there won't be any name conflicts with names in other packages,
- Using packages, it is easier to provide access control and it is also easier to locate the related classes.
Uses of Packages
Packages are used for:
- Preventing naming conflicts. For example, there can be two classes with the name Employee in two packages, college.staff.cse.Employee and college.staff.ee.Employee
- Making searching/locating and usage of classes, interfaces, enumerations and annotations easier
- Providing controlled access: protected and default have package level access control.
- A protected member is accessible by classes in the same package and its subclasses.
- A default member (without any access specifier) is accessible by classes in the same package only.
- Packages can be considered as data encapsulation (or data-hiding).
Types of Packages in Java
Based on whether the package is defined by the user or not, packages are divided into two categories:
1)Built-in Packages
2) User-Defined Packages
1)Built-in Packages
Built-in packages or predefined packages are those that come along as a part of JDK (Java Development Kit) to simplify the task of Java programmers. They consist of a huge number of predefined classes and interfaces that are a part of Java API. One of the commonly used built-in packages is java.lang, java.io, java.util, java.applet, etc. Here’s a simple program using a built-in package.
package Edureka;
import java.util.ArrayList;
class BuiltInPackage {
public static void main(String[] args) {
ArrayList<Integer> myList = new ArrayList<>(3);
myList.add(3);
myList.add(2);
myList.add(1);
System.out.println("The elements of list are: " + myList);
}
}
Output:
The elements of the list are: [3, 2, 1]
The ArrayList class belongs to java.util package. To use it, we have to import the package using the import statement. The first line of the code import java.util.ArrayList imports the java.util package and uses ArrayList class which is present in the subpackage util.
2) User Defined Packages
User-defined packages are those which are developed by users in order to group related classes, interfaces, and sub-packages. With the help of an example program, let’s see how to create packages, compile Java programs inside the packages and execute them.
Or,
1) Built-in Packages
These packages consist of a large number of classes that are a part of Java API. Some of the commonly used built-in packages are:
1) java.lang: Contains language support classes(e.g classed which defines primitive data types, math operations). This package is automatically imported.
2) java.io: Contains classed for supporting input / output operations.
3) java.util: Contains utility classes that implement data structures like Linked List, Dictionary and support; for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java. awt: Contain classes for implementing the components for graphical user interfaces (like button , ; menus etc).
6) java.net: Contain classes for supporting networking operations.
2)User-defined packages
These are the packages that are defined by the user. First, we create a directory myPackage (name should be the same as the name of the package). Then create the MyClass inside the directory with the first statement being the package names.
// Name of the package must be the same as the directory
// under which this file is saved
package myPackage;
public class MyClass
{
public void getNames(String s)
{
System.out.println(s);
}
}
Now we can use the MyClass class in our program.
/* import 'MyClass' class from 'names' myPackage */
import myPackage.MyClass;
public class PrintName
{
public static void main(String args[])
{
// Initializing the String variable
// with a value
String name = "GeeksforGeeks";
// Creating an instance of class MyClass in
// the package.
MyClass obj = new MyClass();
obj.getNames(name);
}
}
Note : MyClass.java must be saved inside the myPackage directory since it is a part of the package.
Comments
Post a Comment