0% found this document useful (0 votes)
5 views30 pages

9 Packages

The document discusses Java packages, which are containers for classes that help organize code, avoid naming conflicts, and improve reusability. It explains how to define packages, find them using the CLASSPATH, and the importance of access protection and importing packages. Additionally, it provides examples of creating and executing Java classes within packages.

Uploaded by

sindhus121985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

9 Packages

The document discusses Java packages, which are containers for classes that help organize code, avoid naming conflicts, and improve reusability. It explains how to define packages, find them using the CLASSPATH, and the importance of access protection and importing packages. Additionally, it provides examples of creating and executing Java classes within packages.

Uploaded by

sindhus121985
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

MODULE 4

Packages and Exceptions

Chapter 9&10
Packages 2

 Packages are containers for classes that are used to keep the class name space
compartmentalized.

 Packages help in organizing large codebases by providing a way to group


related classes and interfaces together.

 They also help in avoiding naming conflicts between classes and interfaces
with the same name but defined in different packages.

 For example, a package allows you to create a class named List, which you can
store in your own package without concern that it will collide with some other
class named List stored elsewhere.

 Packages are stored in a hierarchical manner and


are explicitly imported into new class definitions.
3

 A Package is a collection of related classes.

 It helps organize your classes into a folder structure and make


it easy to locate and use them.

 More importantly, it helps improve re-usability.

 Each package in Java has its unique name and organizes its
classes and interfaces into a separate namespace, or name group
4

•A java package is a group of similar types of classes, interfaces


and sub-packages.
•Package in java can be categorized in two form, built-in package
and user-defined package.
•There are many built-in packages such as java, lang, awt, javax,
swing, net, io, util, sql etc.
5
Defining a Package 6

 To create a package is quite easy: simply include a package


command as the first statement in a Java source file.

 Any classes declared within that file will belong to the specified
package.

 The package statement defines a name space in which classes are


stored.

 If you omit the package statement, the class names are put into the
default package, which has no name.
7

Contd..
• This is the general form of the package statement:

Syntax: package pkg;


Ex: package MyPackage;

Demo.java
package MyPackage;
Java uses file system directories to store
packages class exp{
}

class Demo{
public static void main(String args[]){

}
}
8

Contd..
Java uses file system directories to store packages

Demo.java
package MyPackage;

class exp{
} Try.java
package MyPackage;
class Demo{
public static void main(String args[]){ class xyz{
… }
}
} class Try{
public static void main(String a

}
}
9

Contd..
• You can create a hierarchy of packages. To do so, simply separate
each package name from the one above it by use of a period.
• The general form of a multileveled package statement
is shown here:

package pkg1[.pkg2[.pkg3]];

Cannot rename a package without renaming directories


Finding Packages and CLASSPATH

 packages are mirrored by directories.


 This raises an important question: How does the Java run-time
system know where to look for packages that you create?
The answer has three parts.
1. First, by default, the Java run-time system uses the current
working directory as its starting point. Thus, if your package is in
a subdirectory of the current directory, it will be found.
2. Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.
3. Third, you can use the -classpath option with java and javac to
specify the path to your classes.
Find Packages & Classpath 11

Eg. package MyPack;


 How does the java run time know where to look for the packages
you create
1. Current working directory
• C:\MyPrograms\Java\Mypack
• Here Program can be executed from a directory above MyPack
2. Specify a directory path by setting CLASSPATH
environmental variable
• C:\MyPrograms\Java
3. Use –classpath option with java and javac to specify paths to
classes
• C:\MyPrograms\Java
12
Example
package MyPack; 13

class Balance
class AccountBalance {
{ public static void main(String args[])
String name;
double bal;
{
Balance(String n, double b) Balance current[] = new Balance[3];
current[0] = new Balance("Fielding", 12.23);
{ current[1] = new Balance("Will", 157.02);
name = n; current[2] = new Balance("Tom ", -12.33);
bal = b; for(int i=0; i<3; i++)
}
void show() current[i].show();
}
{ }
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" +
bal);
}
}
14

 Call this file AccountBalance.java and put it in a directory


called MyPack.
 Executes AccountBalance class using
 java Mypack.AccountBalance
 Java directory must above MyPack
 java AccountBalance cannot use, it should be qualified with its
package name.
Access Protection
15

 Packages add another dimension to access control.

Class Member
Access
18

//file name Protection.java


package p1;

}
19

//file name Derived.java


package p1;

}
20

// file name SamePackage.java


Package p1;

}
To test file for package p1

// Demo package p1.


package p1;
// Instantiate the various classes in p1.
public class Demo {
public static void main(String args[])
{
Protection ob1 = new Protection();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}
}
Creating these files, Compiling and executing

– Create a folder named P1


– Navigate to P1
– Create Protection.java mentioning package as P1
– Create Derived.java mentioning package as P1
– Create SamePackage.java mentioning package as P1
– Create Demo.java mentioning package as P1.
– Move the parent directory of P1 (cd ..)
– Compile all the files as javac P1/*.java
– Run the file as java -classpath /root P1.Demo
23

//file name Protection2.java

package p2;
24

//file name Otherpackage.java


package p2;
To test file for package p2

// Demo package P2.


package P2;
// Instantiate the various classes in p2.
public class Demo {
public static void main(String args[]) {
Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();
}
}
Creating these files, Compiling and executing

– Create a folder named P2


– Navigate to P2
– Create Protection2.java mentioning package as P2
– Create OtherPackage.java mentioning package as P2
– Create Demo.java mentioning package as P2.
– Move the parent directory of P2(cd ..)
– Compile all the files as javac P2/*.java
– Run the file as java -classpath /root P2.Demo
27

Importing Packages
 Since classes within packages must be fully qualified with their
package name or names, it could become tedious to type in the
long dot-separated package path name for every class you want to
use.
 Java includes the import statement to bring certain classes, or
entire packages, into visibility. Once imported, a class can be
referred to directly, using only its name.
 In a Java source file, import statements occur immediately
following the package statement (if it exists) and before any class
definitions.
Importing Packages
• the import statement is optional.
• Any place you use a class name, you can use its
fully qualified name, which includes its full package
hierarchy.
• example, this fragment uses an import statement:
import java.util.*;
class MyDate extends Date {
}

• The same example without the import statement


looks like this:
class MyDate extends java.util.Date { }
2
9
import mypack.*;
class TestBalance {
public static void main(String[] args) {
Balance test = new Balance("J. J. Jaspers", 99.88);
test.show(); // you may also call show()
}
}

You might also like