18CS653 Module 4
18CS653 Module 4
18CS653
MODULE-4
PACKAGES , INTERFACE ,
EXCEPTION HANDLING
TOPICS
Packages
Defining a Package
Finding Packages and CLASSPATH
A Short Package Example
Access Protection
An Access Example
Importing Packages
Interfaces
Defining an Interface
Implementing Interfaces
Nested Interfaces
Applying Interfaces
Variables in Interfaces
Interfaces Can Be Extended
TOPICS
Exception-Handling Fundamentals
Exception Types
Uncaught Exceptions
Using try and catch
Displaying a Description of an Exception
Multiple catch Clauses
Nested try Statements
throw
throws
finally
Java’s Built-in Exceptions
Creating Your Own Exception Subclasses
Chained Exceptions
Using Exceptions
Packages
Example: In Java we can create two files with same name in different
packages.
Packages are used to avoid name conflicts, and to write a better maintainable
code.
Example:
package myPack; // myPack is the name of the package
we can create a hierarchy of packages. To do so, simply separate each package
name from the one above it by use of a period.
package java.awt.image;
Finding Packages and CLASSPATH
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.
Example
Access Protection
To import the java package into a class, we need to use the java import
keyword
import keyword is used to access the package and its classes into the java
program.
We use import to access built-in and user-defined packages into our java
source file to refer to a class in another package by directly using its name.
syntax:
import package.name.ClassName; // To import a certain class only
import package.name.* // To import the whole package
Example:
import java.util.Date; // imports only Date class
import java.io.*; // imports everything inside java.io package
Example
Interfaces
interface keyword is used to make a class fully abstract
interface specify that a class must do ,but, not how it does it.
interfaces are similar to classes but, they lack instance variable.
Methods in the interface are declared without body.
The classes that implement the interfaces implement the methods that are
declared in the interface.
The classes that implement the interface can also have their own methods.
One class can implement any number of interfaces.
Defining an Interfaces
An interface is defined much like a class. This is the general form of an
interface:
When no access specifier is included, then default access results, and the
interface is only available to other members of the package in which it is
declared.
Defining an Interfaces
Variables can be declared inside of interface declarations. They are
implicitly final and static, meaning they cannot be changed by the
implementing class.
They must also be initialized.
All methods and variables are implicitly public.
Example:
interface Callback
{
void callback(int param);
}
Implementing an Interfaces
Once an interface has been defined, one or more classes can implement
that interface.
To implement an interface, include the implements clause in a class
definition, and then create the methods defined by the interface.
The general form of a class that includes the implements clause looks like
this:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
If a class implements more than one interface, the interfaces are separated
with a comma. If a class implements two interfaces that declare the same
method, then the same method will be used by clients of either interface.
Example
Example
Nested Interface
An interface, i.e., declared within another interface or class, is known as a
nested interface.
Nested interface can be declared as private , protected or public.
When a nested interface is used outside of its enclosing scope, it must be
qualified by the name of the class or interface of which it is a member.