36
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10

Lecture 11

  • Upload
    rhonda

  • View
    31

  • Download
    2

Embed Size (px)

DESCRIPTION

Lecture 11. Interfaces and Exception Handling from Chapters 9 and 10. Lecture Review. Introduction to Interfaces Implementing and Applying Interfaces Exception Handling Exception Types Statements – try, catch, throw, finally Java’s built-in exceptions. Interface. - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 11

1

Lecture 11

Interfaces and Exception Handling from Chapters 9 and 10

Page 2: Lecture 11

2

Lecture Review

Introduction to Interfaces

Implementing and Applying Interfaces

Exception Handling

Exception Types

Statements – try, catch, throw, finally

Java’s built-in exceptions

Page 3: Lecture 11

3

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.

Page 4: Lecture 11

4

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;

}

Page 5: Lecture 11

5

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.

Page 6: Lecture 11

6

An example

A simple interface called callback().

Interface Callback {Void callback(int param);

}

Page 7: Lecture 11

7

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.

Page 8: Lecture 11

8

More about Interface (2) - from Clayton Walnum

Multiple inheritance means creating a new class that inherits behavior directly from more than one superclass.

Page 9: Lecture 11

9

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.

Page 10: Lecture 11

10

Example of Interface

Page 11: Lecture 11

11

Example – result

Page 12: Lecture 11

12

More Example

Page 13: Lecture 11

13

One interface, two implementations

Page 14: Lecture 11

14

Explanation

AInterface

CClass RClass

Page 15: Lecture 11

15

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.

Page 16: Lecture 11

16

Example – Herbert, page 246

A B

MyClass – 3 methods

Page 17: Lecture 11

17

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.

Page 18: Lecture 11

18

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.

Page 19: Lecture 11

19

General Form of an exception-handling

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}

Page 20: Lecture 11

20

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;

int a = 12/d;

}

}

Divide by 0

Page 21: Lecture 11

21

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.

Page 22: Lecture 11

22

Example – try and catch, divide by zero

Page 23: Lecture 11

23

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

Page 24: Lecture 11

24

Example – try and catch, not divide by zero, note there is no error

Page 25: Lecture 11

25

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.

Page 26: Lecture 11

26

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.

Page 27: Lecture 11

27

Example of multiple catch

Page 28: Lecture 11

28

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.

Page 29: Lecture 11

29

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

Page 30: Lecture 11

30

Example

Page 31: Lecture 11

31

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.

Page 32: Lecture 11

32

Example

Page 33: Lecture 11

33

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.

Page 34: Lecture 11

34

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.

Page 35: Lecture 11

35

Example

Page 36: Lecture 11

36

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