Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10."— Presentation transcript:

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

2 2 Lecture Review Introduction to Interfaces Implementing and Applying Interfaces Exception Handling Exception Types Statements – try, catch, throw, finally Java’s built-in exceptions

3 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 can specify what a class must do, but not how it does. Interfaces are syntactically similar to classes, but lack of instance variables.

4 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; }

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

6 6 An example A simple interface called callback(). Interface Callback { Void callback(int param); }

7 7 More about Interface (1) - from Clayton Walnum 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.

8 8 More about Interface (2) - from Clayton Walnum Multiple inheritance means creating a new class that inherits behavior directly from more than one superclass.

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

10 10 Example of Interface

11 11 Example – result

12 12 More Example

13 13 One interface, two implementations

14 14 Explanation AInterface CClassRClass

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

16 16 Example – Herbert, page 246 AB MyClass – 3 methods

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

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

19 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}

20 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

21 21 Using try and catch Although the default exception handler provided by the java run-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.

22 22 Example – try and catch, divide by zero

23 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

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

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

26 26 Multiple Catch Clauses 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.

27 27 Example of multiple catch

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

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

30 30 Example

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

32 32 Example

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

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

35 35 Example

36 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

Download ppt "1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10."

Similar presentations

Ads by Google