7 Java API Packages - Package Access
7 Java API Packages - Package Access
Engineering
Course Code: E1UC307C Course Name:Java Programming
1
Faculty Name: Jitender Tanwar Programe Name: B.Tech
Package in Java
Package is a mechanism to encapsulate a group of classes, sub packages and
interfaces.
OR
Package is a pack/container of classes, interfaces and other packages.
Name Conflicts: We can define two classes with the same name in different
packages so to avoid name collision, we can use packages
3
Type of packages in Java
Two types of packages in java.
4
Predefined/Built-In packages
NOTE:
Core packages of java starts with java. (For example: java.lang.*)
5
In java we have several built-in packages, for example when we need
user input, we import a package like this:
import java.util.Scanner;
Here:
→ java is a top level package
→ util is a sub package
→ and Scanner is a class which is present in the sub package util.
6
Program to receive user input using util package
import java.util.Scanner;
class GetInputData
{
public static void main(String args[])
{
int num; float fnum; String str;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string: "); //Get input String
str = in.nextLine();
System.out.println("Input String is: "+str);
System.out.println("Enter an integer: "); //Get input Integer
num = in.nextInt();
System.out.println("Input Integer is: "+num);
System.out.println("Enter a float number: "); //Get input float number
fnum = in.nextFloat();
7
System.out.println("Input Float number is: "+fnum); } }
Java Built-in packages
As a part of J2SE we have nine predefined packages which are given
in the following table:
8
Package name Package description
9
Package Package description
name
10
User defined package
Syntax:
package pack1[.pack2[.pack3……[.packn]…..]];
Here, package is a keyword which is used for creating user defined packages.
pack1 represents upper package and pack2 to packn represents sub
packages.
11
Package example
For example:
package p1; // statement-1
package p1.p2; // statement-2
The statements 1 and 2 are called package statements.
RULE:
Whenever we create user defined package statement as a part of java
program, we must use package statement as a first executable
statement.
12
NONAME package
Whenever we develop any JAVA program it contains ‘n’ number of classes
and interfaces.
Each and every class and interface which are developed by the programmer
must belong to a package (as per industry standards).
If the programmer is not keeping the set of classes and interfaces in a
package, JVM will assume its own package called NONAME package.
NONAME package will exist only for a limited span of time until the
program is completing.
13
Steps for developing a package
i. Choose the appropriate package name, the package name must be a JAVA
valid variable name and we showed ensure the package statement must
be first executable statement.
ii. Choose the appropriate class name or interface name and whose
modifier must be public.
iii. The modifier of Constructors of a class must be public.
iv. The modifier of the methods of class name or interface name
must be public.
v. At any point of time we should place either a class or an interface in a
package and give the file name as class name or interface name with
extension .java Suppose your interface is ABC then save file as
“ABC.java”.
14
Example: Java package
17
How to use PACKAGE
How to use PACKAGE CLASSES and INTERFACES in another java
program:
In order to refer package classes and interfaces in JAVA we have two
approaches, they are
1. Using import statement
2. Using fully qualified name approach.
For example:
Import p1.c1; ---4
Import p1.p2.c3; ---5
26
Thank you
27
References:
https://www.geeksforgeeks.org/
https://www.javatpoint.com/exception-handling-in-java
https://www.tutorialspoint.com/java/java_exceptions.htm
The complete reference, eleventh edition, available at:
https://gfgc.kar.nic.in/sirmv-science/GenericDocHandler/1
38-a2973dc6-c024-4d81-be6d-5c3344f232ce.pdf
28