Module 4:Packages and Interfaces Programming in Java (18CS653)
Module 4
Packages and Interfaces, Exception Handling
Packages and Interfaces:
Packages,
Access Protection,
Importing Packages,
Interfaces,
Exception Handling:
Exception-Handling Fundamentals,
Exception Types,
Uncaught Exceptions,
Using try and catch,
Multiple catch Clauses,
Nested try Statements,
throw, throws, finally,
Java’s Built-in Exceptions,
Creating Your Own Exception Subclasses,
Chained Exceptions,
Using Exceptions.
Mrs.Mamatha A, Asst. Prof, Dept., of CSE, SVIT 1
,Module 4:Packages and Interfaces Programming in Java (18CS653)
Packages and Interfaces:
Packages:
The name of each example class was taken from the same name space. This means
that a unique name had to be used for each class to avoid name collisions.
Java provides a mechanism for partitioning the class name space into more
manageable chunks. This mechanism is the package.
The package is both a naming and a visibility control mechanism. You can define
classes inside a package that are not accessible by code outside that package. You can
also define class members that are only exposed to other members of the same
package.
Defining a Package:
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.
This is the general form of the package statement:
package pkg;
Here, pkg is the name of the package.
For example, the following statement creates a package called MyPackage.
package MyPackage;
Java uses file system directories to store packages. For example, the .class files for
any classes you declare to be part of MyPackage must be stored in a directory called
MyPackage.
Remember that case is significant, and the directory name must match the package
name exactly.
More than one file can include the same package statement. The package statement
simply specifies to which package the classes defined in a file belong.
Mrs.Mamatha A, Asst. Prof, Dept., of CSE, SVIT 2
, Module 4:Packages and Interfaces Programming in Java (18CS653)
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]];
A package hierarchy must be reflected in the file system of your Java development
system.
For example, a package declared as
package java.awt.image;
Finding Packages and CLASSPATH:
Packages are mirrored by directories.
The Java run-time system know where to look for packages that you create
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.
Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.
Third, you can use the -classpath option with java and javac to specify
the path to your classes.
For example, consider the following package specification:
package MyPack
can be executed from a directory immediately above MyPack, or the CLASSPATH must be
set to include the path to MyPack, or the -classpath option must specify the path to MyPack
when the program is run via java.
When the second two options are used, the class path must not include MyPack,
itself.
It must simply specify the path to MyPack. For example, in a Windows environment,
if the path to MyPack is
C:\MyPrograms\Java\MyPack
Then the class path to MyPack is
C:\MyPrograms\Java
Mrs.Mamatha A, Asst. Prof, Dept., of CSE, SVIT 3
Module 4
Packages and Interfaces, Exception Handling
Packages and Interfaces:
Packages,
Access Protection,
Importing Packages,
Interfaces,
Exception Handling:
Exception-Handling Fundamentals,
Exception Types,
Uncaught Exceptions,
Using try and catch,
Multiple catch Clauses,
Nested try Statements,
throw, throws, finally,
Java’s Built-in Exceptions,
Creating Your Own Exception Subclasses,
Chained Exceptions,
Using Exceptions.
Mrs.Mamatha A, Asst. Prof, Dept., of CSE, SVIT 1
,Module 4:Packages and Interfaces Programming in Java (18CS653)
Packages and Interfaces:
Packages:
The name of each example class was taken from the same name space. This means
that a unique name had to be used for each class to avoid name collisions.
Java provides a mechanism for partitioning the class name space into more
manageable chunks. This mechanism is the package.
The package is both a naming and a visibility control mechanism. You can define
classes inside a package that are not accessible by code outside that package. You can
also define class members that are only exposed to other members of the same
package.
Defining a Package:
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.
This is the general form of the package statement:
package pkg;
Here, pkg is the name of the package.
For example, the following statement creates a package called MyPackage.
package MyPackage;
Java uses file system directories to store packages. For example, the .class files for
any classes you declare to be part of MyPackage must be stored in a directory called
MyPackage.
Remember that case is significant, and the directory name must match the package
name exactly.
More than one file can include the same package statement. The package statement
simply specifies to which package the classes defined in a file belong.
Mrs.Mamatha A, Asst. Prof, Dept., of CSE, SVIT 2
, Module 4:Packages and Interfaces Programming in Java (18CS653)
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]];
A package hierarchy must be reflected in the file system of your Java development
system.
For example, a package declared as
package java.awt.image;
Finding Packages and CLASSPATH:
Packages are mirrored by directories.
The Java run-time system know where to look for packages that you create
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.
Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.
Third, you can use the -classpath option with java and javac to specify
the path to your classes.
For example, consider the following package specification:
package MyPack
can be executed from a directory immediately above MyPack, or the CLASSPATH must be
set to include the path to MyPack, or the -classpath option must specify the path to MyPack
when the program is run via java.
When the second two options are used, the class path must not include MyPack,
itself.
It must simply specify the path to MyPack. For example, in a Windows environment,
if the path to MyPack is
C:\MyPrograms\Java\MyPack
Then the class path to MyPack is
C:\MyPrograms\Java
Mrs.Mamatha A, Asst. Prof, Dept., of CSE, SVIT 3