1 / 36

Lecture 11

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.

rhonda
Download Presentation

Lecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11 Interfaces and Exception Handlingfrom Chapters 9 and 10

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

  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. 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. An example • A simple interface called callback(). Interface Callback { Void callback(int param); }

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

  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. 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. interface: defines it not to implement

  10. Example of Interface interface implement

  11. Example – result

  12. More Example Interface Implement Result

  13. One interface, two implementations Interface Implement Result

  14. Explanation AInterface area() Ainterface defines area() which is implemented by CClass and RClass. CClass RClass PI.r.r w.h Implement

  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. Example – Herbert, page 246 A B MyClass – 3 methods test class

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

  22. Example – try and catch, divide by zero 1 2 3 4 result

  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. Example – try and catch, not divide by zero, note there is no error 1 2 3 result

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

  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.

  27. Example of multiple catch result no argument one argument

  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. 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. Example no argument one argument two argument

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

  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. 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. Example If a finally block is associated with a try, a finally block will be executed upon try block. result

  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

More Related