Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

7 Java API Packages - Package Access

The document provides an overview of Java packages, explaining their purpose, types, and advantages, such as reusability and better organization of code. It details predefined and user-defined packages, along with examples of how to create and use them in Java programs. Additionally, it covers the syntax for compiling and importing packages, as well as access specifiers applicable to packages.

Uploaded by

okokji4201
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

7 Java API Packages - Package Access

The document provides an overview of Java packages, explaining their purpose, types, and advantages, such as reusability and better organization of code. It details predefined and user-defined packages, along with examples of how to create and use them in Java programs. Additionally, it covers the syntax for compiling and importing packages, as well as access specifiers applicable to packages.

Uploaded by

okokji4201
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Name of the School: School of Computer Science and

Engineering
Course Code: E1UC307C Course Name:Java Programming

DEPARTMENT OF COMPUTER SCIENCE


& ENGINEERING
Subject Name: Java Programming
Topics Covered: Java Packages

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.

In java we use packages to organize our classes and interfaces.


We have two types of packages in Java:
1. Pre-Defined/Built-in/Java API packages
2. User defined package
By default one predefined package is imported for each and every JAVA
program and whose name is java.lang.*.
2
Advantage of using a package in Java
These are the reasons why you should use packages in Java:
Reusability: While developing a project in java, we often feel that there are
few things that we are writing again and again in our code. Using packages,
you can create such things in form of classes inside a package and whenever
you need to perform that same task, just import that package and use the class.
Better Organization: Again, in large java projects where we have several
hundreds of classes, it is always required to group the similar types of classes
in a meaningful package name so that you can organize your project better and
when you need something you can quickly locate it and use it, which improves
the efficiency.

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.

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.

4
Predefined/Built-In packages

Predefined packages are those which are developed by SUN micro


systems and supplied as a part of JDK (Java Development Kit) to
simplify the task of java programmer.

NOTE:
Core packages of java starts with java. (For example: java.lang.*)

Advanced packages of java starts with javax. (For example:


javax.sql.*)

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:

Package name Package description

java.lang.* This package is used for achieving the language


functionalities such as convertion of data from string to
fundamental data, displaying the result on to the console,
obtaining the garbage collector.
This is the package which is by default imported for each
and every java program.

8
Package name Package description

java.awt.event.* Event is the sub package of awt package.


This package is used for providing the functionality to GUI
components, such as, when button is clicked or when check box is
checked, when scroll box is adjusted either vertically or
horizontally.

java.applet.* This package is used for developing browser oriented applications.


In other words this package is used for developing distributed
programs.
An applet is a java program which runs in the context of www or
browser.

9
Package Package description
name

java.net.* This package is used for developing client server applications.

java.util.* This package is used for developing quality or reliable applications in


java or J2EE.

This package contains various classes and interfaces which improves


the performance of J2ME applications.
This package is also known as collection framework (collection
framework is the standardized mechanism of grouping of similar or
different type of objects into single object.

This single object is known as collection object).

10
User defined package

A user defined package is one which is developed by java programmers to simplify


the task of the java programmers to keep set of classes, interfaces and sub packages
which are commonly used.
Any class or interface is commonly used by many java programmers that class or
interface must be placed in packages.

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

package pack; The -d switch specifies the


destination where to put the
public class Calculator {
generated class file. You can
public int add(int a, int b){ use any directory name
return a+b; like d:/Atul (in case of windows)
} etc. If you want to keep the
package within the same
public static void main(String args[]){ directory, you can use . (dot).
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
}}
Note: Compilation: javac –d . Calculator.java
This command forces the compiler to create a package.
The "." operator represents the current working directory.
15
To execute the program java pack.Calculator
Package Name of file
// Test.java //ITest.java
package tp;
package tp; public interface ITest
public class Test {
{ void disp ();
public Test () }
{
System.out.println ("TEST – DEFAULT CONSTRUCTOR");
}
public void show ()
{
System.out.println ("TEST - SHOW");
}
16 }
Syntax for compiling a package
javac –d . filename.java
For example:
javac –d . Test.java
Here, -d is an option or switch which gives an indication to JVM saying that
go to Test.java program take the package name and that package name is
created as directory automatically provides no errors are present in Test.java.
When Test.java is not containing any errors we get Test class file and it will
be copied automatically into current directory which is created recently i.e.,
tp (package name).
The above program cannot be executed since it doesn’t contain any main
method.

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.

Using import statement:

Import is a keyword which is used to import either single class or interface


18 or set of classes and interfaces all at once.
Import Package
Syntax -1:
Import pack1 [.pack2 [.………[.packn]]].*;
For example:
Import p1.*; ---1
Import p1.p2.*; ---2
Import p1.p2.p3.*; ---3
When statement 1 is executing we can import or we can access all the classes
and interfaces of package p1 only but not its sub packages p2 and p3 classes and
interfaces.
When statement 2 is executing we can import as the classes and interfaces of
package p2 only but not p1 and p3 classes and interfaces.
When statement 3 is executing we can import as the classes and interfaces of
19
package p3 only but not p1 and p2 classes and interfaces.
Import package
Syntax-2:
Import pack1 [.pack2 [.…………[.packn]]].class name/interface name;

For example:
Import p1.c1; ---4
Import p1.p2.c3; ---5

When statement 4 is executing can import c1 class of package p1 only


but not other classes and interfaces of p1 package, p2 package and p3
package.
20
JAVA program which illustrates the usage of package
classes When we compile the program we get the
//Import approach: following error “package tp does not
exist”. To avoid the above error we must set
import tp.Test;
the classpath as., SET CLASSPATH =
class PackDemo %CLASSPATH%;.;
{
public static void main (String [] args)
{
Test t1=new Test ();
t1.show ();
}
};
21
This is the alternate technique for import statement:
p1.c2 o2=new p1.c2 ();
p1.p2.p3.c4 o4=new p1.p2.p3.c4 ();
p1.p2.i3 o3=new p1.p2.p3.c4 ();
Fully qualified approach:
class PackDemo
{
public static void main (String [] args)
{
tp.Test t1=new tp.Test ();
t1.show ();
}
22 };
Program
package pk;
public class First{
public void msg() {
System.out.println("Hello");
}
} // save this program in separate file and compile
it ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
// Second program
import pk.*;
class Second {
public static void main(String args[]) {
pk.Firstp obj = new pk.Firstp();
obj.msg();
23
} }
Program : Both are in package
package pk;
public class First{
public void msg() {
System.out.println("Hello");
}
}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
package aviJava;
import pk.*;
class Second {
public static void main(String args[]) {
pk.Firstp obj = new pk.Firstp();
obj.msg();
}
24
}
Note
1. Whenever we develop user defined packages, to use the classes and
interfaces of user defined packages in some other program, we must set
class path before there usage.
2. In order to set the classpath for predefined packages we must use the
following statement:

D:\core\set classpath=C: \Program Files\Java\jdk1.5.0\lib\rt.jar;.;


[rt.jar contains
all the .class files for the predefined classes which are supplied as a
port of predefined packages by the SUN micro systems.]
25
Access Specifiers applicable in a
package

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

You might also like