Lecture 11
1 / 36

Lecture 11 - PowerPoint PPT Presentation

  • Uploaded on

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Lecture 11' - rhonda

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lecture 11

Lecture 11

Interfaces and Exception Handlingfrom Chapters 9 and 10

Lecture review
Lecture Review

  • Introduction to Interfaces

  • Implementing and Applying Interfaces

  • Exception Handling

  • Exception Types

  • Statements – try, catch, throw, finally

  • Java’s built-in exceptions


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

Defining an interface
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;


Explanation to the definition
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.

An example
An example

  • A simple interface called callback().

    Interface Callback {

    Void callback(int param);


More about interface 1 from clayton walnum http docs rinet ru kofeynyyprimer ch29 htm interfaces
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.

More about interface 2 from clayton walnum
More about Interface (2) -from Clayton Walnum

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

Summary of interface
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

Example of interface
Example of Interface



More example
More Example




One interface two implementations
One interface, two implementations







Ainterface defines area()

which is implemented

by CClass and RClass.






Interface can be extended
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.

Example herbert page 246
Example – Herbert, page 246



MyClass –

3 methods

test class

Exception handling mechanism page 250
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.

Exception handling in java
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.

General form of an exception handling
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}

An example uncaught exceptions
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

Using try and catch
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.


  • 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

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





Lecture 11

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.


Multiple catch causes
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.

Example of multiple catch
Example of multiple catch


no argument

one argument


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

Nested try statements
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.)


no argument

one argument

two argument


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


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


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


If a finally block is associated

with a try, a finally block will

be executed upon try block.



  • 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