Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces. A java
package is a group of similar types of classes, interfaces and sub-packages.
Packages are used for:
- Preventing naming conflicts. For example there can be two classes with name Employee in two packages.
- 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 thesame 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
1) User defined package: The package we create is called user-defined package.
2) Built-in package: The already defined package like java.io.*, java.lang.* etc are known as built-in packages.
Built-in Packages
The Java API is a library of prewritten classes that are free to use, included in the Java Development Environment.
The library contains components for managing input, database programming, and much much more.
The library is divided into packages and classes. That is either a single class (along with its methods and attributes) is imported , or a whole package that contain all the classes that belong to the specified package can be impoted.
To use a class or a package from the library, you need to use the import keyword:
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
User-defined Packages
To create user defined package, Java uses a file system directory to store them.
└── root
└── mypackage
└── MyPackageClass.java
To create a package, “package” keyword is used.
Ex:
package mypackage;
class MyPackageClass {
public static void main(String[] args) {
System.out.println("This is my package!");
}
}
Follow the syntax given below to compile:
javac -d directory javafilename
For example
javac -d . MyPackageClass.java
The -d switch specifies the destination where to put the generated class file. Any directory name like /home (in case of Linux), d:/abc (in case of windows) etc can be used. To keep the package within the
same directory, use . (dot).
There are three ways to access the package from outside the package.
- import package.*;
- import package.classname;
- fully qualified name.
If package.* is used then all the classes and interfaces of this package will be accessible but not subpackages.
The import keyword is used to make the classes and interface of another package accessible to the current package.
If fully qualified name is used then only declared class of this package will be accessible. There is no need to import, but have to use fully qualified name every time when class or interface is accessed.
Package inside the package is called the subpackage. If package is imported, all the classes and interface of that package will be imported excluding the classes and interfaces of the subpackages. The import has to be used to import classes of subclasses as well.
0 comments:
Post a Comment