How Packages In Java are useful?
- This is useful for code reusability.
- thus, using the same functions and methods for different complex calculations.
- Can create user-defined packages.
- though Can import different packages in a program.
- Can also retrieve user input.
Packages in Java
A package is a collection of similar types of classes, abstract classes, thus interfaces and sub-packages, and more.
Type of package
The packages in Java can be defined into two types which are ready below.
- Predefined or built-in package.
- User-defined package.
1. Predefined or built-in package
These are the packages which are already designed by the Sun Microsystem(oracle) and supplied as a part of java VM. thus, every predefined package is a collection of predefined classes, interfaces, and sub-package.
- java. lang − bundles the fundamental classes
- ·java.io − classes for input, output functions are bundled in this package
2. User defined package
If any package is defined by the user then it is known as a user-defined package. however, User-defined packages are those which are developed by a user, java programmer, or developer and submitted as a part of their project to deal with common requirements.
Creating a package:
To create a package include a package command as the first statement in a Java file. so, Any classes defined within that file will only belong to that individual package.
though, If you omit that package name, the class names are put into the present default package, which has no declaration.
Syntax: package pkg;
Example: package mypackage;
We can create a hierarchy of packages. To do so, simply separate each package name from each one,
Syntax: package pkg1[.pkg2[.pkg3]];
Eg: package java.awt.image;
Example:
Importing packages:
Java includes the import statement to import particular classes, or entire packages, into a java program. Once imported, a class can be defined by its name.
import mypackage1.A;
public class Hello
{
public static void main(String arg1[])
{
A1 a=new A1();
a.show1();
System.out.println(“show1() class A”);
}
}
Using Package Members
We can declare package members in the following ways :
- Refer to the member by its fully qualified name
- Import the package member
- Import the member’s entire package
Scope of packages
packages can also be defined in the following ways
- Public: the public packages and their members can access by any program and user, Example of this are packages java VM (they have inbuilt public packages).
- Private: the private packages cannot be accessed by every program, it can only be accessed by the privileged user.
Compiling of package programs:
> Javac -d . <java filename>.java
Example of creating and deploying a package:-
- Calculating marks & percentage
- Create a package containing calculations of marks
import this package in the main program by an import statement
(Name this file as StudentMain.java)
Conclusion:-
therefore, Packages are generally useful to import other methods and class into a user program, packages save a lot of time to a developer as it is helpful in two ways:-
- Reusability of code.
- Can import many packages in a single program.
With the help of an import statement, we can Import packages in our program.
however, We can define our own packages called user defined packages, or we can use default packages provided by java for example like java.awt.*.
written by: Rohit Damji
reviewed by: Soutik Maity
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs