Transcript Slide 1

Lecture 11
Interfaces and Exception
Handling from Chapters 9 and 10
1
Lecture Review
Introduction to Interfaces
Implementing and Applying Interfaces
Exception Handling
Exception Types
Statements – try, catch, throw, finally
Java’s built-in exceptions
2
Interface
Based on the description of Herbert, we
can fully abstract a class’s interface from
its implementation.
That is to say, by use of interface, we an
specify what a class must do, but not how
it does.
Interfaces are syntactically similar to
classes, but lack of instance variables.
3
Defining an Interface
An interface is defined much like a class. The
general form of an interface is:
Access interface name {
return-type method-name1 (parameter-list)
return-type method-name2(parameter-list)
….
type final-varname1 = value;
….
type final-varnameN = value;
}
4
Explanation to the definition
Access: it is either public or not used,
when it is declared as public, the interface
can be used by any other.
Name: the name of the interface
Variables: it can be declared inside of
interface declarations. They are implicitly
final and static.
5
An example
A simple interface called callback().
Interface Callback {
Void callback(int param);
}
6
More about Interface (1) -
from Clayton Walnum
http://docs.rinet.ru/KofeynyyPrimer/ch29.htm#Interfaces
Packages are fairly easy to understand, being
little more than a way to organize related classes
whether built-in or developed by programmers.
Interfaces are a concept that is a bit harder to
grasp.
To really understand Java's interfaces, you have
to know about something called multiple
inheritance, which is not allowed in Java, but
which is often used in other object-oriented
languages.
7
More about Interface (2) -
from Clayton Walnum
Multiple inheritance means creating a new
class that inherits behavior directly from
more than one superclass.
8
Summary of Interface
An interface is very much like a class-with
one important difference. None of the
methods declared in an interface are
implemented in the interface itself.
Instead, these methods must be
implemented in any class that uses the
interface.
9
Example of Interface
10
Example – result
11
More Example
12
One interface, two
implementations
13
Explanation
AInterface
CClass
RClass
14
Interface can be extended
One Interface can inherit another by use of
the keyword extends.
When a class implements an interface that
inherits another interface, it must provide
implementation for all methods defined
within the interface inheritance chain.
15
Example – Herbert, page 246
A
B
MyClass –
3 methods
16
Exception Handling Mechanism, page 250
An exception is an abnormal condition that
happens at run time.
It is a run-time error (not compilation error).
Some computer languages that do not
support the use of error code, the user
must check and handle it.
17
Exception Handling in Java
A Java exception is an object that
describes an exceptional condition that
has occurred in a piece of code.
When an exceptional condition arises, an
object representing that exception is
created and thrown in the method that
caused the error.
The five keywords are: try, catch, throw,
throws and finally.
18
General Form of an exceptionhandling
try {
// block of code to monitor for errors
}
catch (Exception Type1 exOb) {
//exception handler for Exception Type 1
}
……
finally {
// block of code to be executed before try block
ends}
19
An example – uncaught exceptions
Look at the following example, without proper
handling, it will cause an error of divide-by-zero.
Class divide {
public static void main(String args[]) {
int d = 0;
Divide by 0
int a = 12/d;
}
}
20
Using try and catch
Although the default exception handler
provided by the java un-time system is
useful, we will usually want to handle
exception.
There are two benefits: 1) It allows us to
fix the error 2) it prevents program from
automatically terminating.
21
Example – try and catch, divide
by zero
22
Explanation
Note that the call to println(“This will not be
printed..”) inside the try block is never
executed.
Once an exception is thrown, program
control transfers out of the try block into
the catch block.
That is to day, once there is an arithmetic
error, it will exit from the try block to the
catch block
23
Example – try and catch, not
divide by zero, note there is no error
24
Example –
a loop is used with two random integers. These two integers
are divided by each other and the result is used to divide the value 11.
25
Multiple Catch Causes
In some cases, more than one exception
could be raised by a single piece of code.
To handle this, we can specify two or more
catch clauses.
Each of them catches a different type of
exception.
26
Example of multiple catch
27
Explanation
When there is no argument, args.length
becomes 0 and b = 12/a becomes an error
of “divide by 0”
When there is an argument, args.length
becomes 1, the program executes c[12]
which is not defined. The program defines
c[] = {1}, one element only.
28
Nested try Statements
The try statement can be nested.
A try statement can be inside the block of
another try.
Each time a try statement is entered, the
context will be put on a stack (a temporary
location.)
29
Example
30
throw
It is possible for our program to throw an
exception explicitly using the throw.
The general form is
throw throwableitem;
Here, throwableitem must be an object of
type that can be throwable or a subclass
of throwable.
31
Example
32
Explanation
The program has two chances to deal with
the same error.
First, main() sets up an exception context
and then calls demoproc().
The demoproc() method then sets up
another exception-handling context and
immediately throws a new instance of
NullPointerException.
33
finally
finally creates a block o code that will be
executed after a try/catch block has
completed and before the code following
the try/catch block.
The finally block will execute whether or
not an exception is thrown.
34
Example
35
Summary
Introduction to Interfaces - we an specify
what a class must do, but not how it does.
Implementing and Applying Interfaces –
interface (defines) others (implement, methods)
Exception Handling – abnormal case, the
general form is try {….} catch{….}
Exception Statements – try, catch, throw,
finally
36