Java Technology Interfaces and Exceptions
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Session Plan
Interfaces in Java
Packages in Java
Need and Advantages of using package
Creating a Package
Using Existing Packages
Java Class Libraries
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
2
Session Plan
Basic concepts of Exception Handling
Errors and Exceptions
Try – Catch – Finally block
Throw & Throws
Some Built-in exceptions
User defined exceptions
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
3
Interfaces in Java
An interface is a set of predefined methods to be implemented by one or more classes in future.
Java achieves multiple inheritance. By providing the interface keyword, Java allows to fully utilize the “one interface, multiple methods” aspect of polymorphism
Interfaces do not have any instance variables. Any variable declared in an interface is automatically taken as public, static and final
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
4
Creating an interface class public interface myinterface { public void method1(int x, int y) ; }
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
5
Implementing Multiple Inheritance in Java Inter Paternal
Inter Maternal Inter GrandParent
INTERFACE Class Child Class Child implements Paternal, Maternal, GrandParent {............; } Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
6
Interfaces …
Comparable to a pure abstract class
Classes implement, by providing definitions to the methods of the interface
Inheritance is possible in interfaces, even multiple inheritance is possible
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
7
Interface vs Abstract classes
Interfaces don’t worry about classes relationships. It just requires that the classes should implement them.
Abstract classes force strict class relationships.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
8
Interface vs Multiple Inheritance
Interfaces are not synonymous with multiple inheritance.
However, a class can implement more than one interface.
So in a way, we can tell interface is substitute for multiple inheritance.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
9
Packages in Java – Why ? What ?
Reusability of code is one of the most important requirements in the software industry.
Reusability saves time, effort and also ensures consistency.
A class once developed can be reused by any number of programs wishing to incorporate the class in that particular program.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
10
Features of Packages
Organize your classes into smaller units and make it easy to locate and use the appropriate class file.
Avoid naming conflicts.
Packages allow you to protect your classes, data and methods in a larger way than on a class-to-class basis.
Package names can be used to identify your classes.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
11
Concept of Packages
In Java, the code which can be reused by other programs is put into a “Package”.
A Package is a collection of classes, interfaces and/or other packages.
Packages are essentially a means of organizing classes together as groups.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
12
Usage of packages:
Package creation
In Java packages are created in the following manner.
Package packagename;
Package member can be accessed using import statements
import package_name. class_name;
My_Package
My_Sub_Package
import packagename,packagename.*;
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
13
My_Class
Importing + Creating a Package
While creating a package, care should be taken that the statement for creating a package must be written before any other import statements
LEGAL
ILLEGAL
package mypackage ;
import java.io;
import java.io;
package mypackage ;
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
14
Standard Java Packages The Three Java Packages that are essential to any Java program are :
java.lang
Contains classes that form the basis of the design of the programming language of Java.
You don’t need to explicitly import this package. It is always imported for you.
The Wrapper classes, System class, Thread class etc, belong to this package.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
15
Standard Java Packages
java.io
Input/Output operations in Java is handled by the java.io package.
java.util
Contains classes and interfaces that provide additional utility.
E.g. : creating lists, calendar, date etc.
java.net
This package provides classes and interfaces for TCP/IP network programming.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
16
Standard Java Packages
java.awt
This package is useful to create GUI applications.
java.applet
This package consists of classes that you need, to execute an applet in the browser or an appletviewer
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
17
Math Class – java.lang package
Math class – which provides mathematical functions like sine, cosine and square root.
Some methods in the Math class (static methods)
int abs(int i) - returns the absolute value of i
double abs(double d) - returns the absolute value of d
double ceil(double d) - returns as a double the smallest integer that is not less than d
double floor(double d) - returns as a double the largest integer
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
18
Exceptions in Java
A Java exception is an object that describes an exceptional (abnormal) condition, that has occurred in a piece of code.
Exceptions can be generated by the Java run-time system, or they can be manually generated by your code.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
19
Hierarchy Object
Throwable
Error Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Exception
20
Exceptions and Errors
Exceptions are situations within the control of an application, that it should try to handle
When a dynamic linking failure or some other “hard” failure in the virtual machine occurs, the virtual machine throws an error.
Java programs need not catch these errors, because these errors are beyond their scope.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
21
Some Java Errors
ClassFormatError
InternalError
LinkageError
OutOfMemoryError
StackOverflowError
UnknownError
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
22
Types of Exceptions Exceptions
Checked Exceptions (Compile time)
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Unchecked Exceptions (Run time)
23
Checked Exceptions
These exceptions occur not because of the fault of programmer. The reasons being, not available I/O, network error, file not found for reading, etc.
These exceptions are checked during compile time. Hence appropriate code has to be written to either catch or throw the exceptions.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
24
Runtime Exceptions
Examples NullpointerException ArrayOutOfBoundsException ArithmeticException
It is the duty programmer to handle these exceptions.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
25
Exception Example
int i=5/0;
What happens when the above code is executed ?
Exception is thrown (an object is thrown)
How to recover from this ? (handle it !)
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
26
Exception Handling Mechanism
Try-catch block
Multiple catch block
Finally
Try-catch-finally block
Try- finally
Throws
Throw
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
27
Try-catch
A try block is always followed by a catch block, which handles or catches the exception. A catch block always monitors the preceding try block. The catch block is not executed if no exception is thrown.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
28
Try-catch block try { ……… ……… ………} catch (exceptionType name) { ……… ……… ………} Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
29
Example Exception class ExceptionExample { public static void main(String argx[]) { int n,i; try { i=0; n=12/i; System.out.println("This line will not be printed"); catch(ArithmeticException e) { System.out.println("Division by zero" + e); } System.out.println("After catch statement"); } } Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
30
}
Multiple Catch try
{ ……… ……… ……… } catch (exceptionType name) { ……… ……… ……… } catch (exceptiontype2 name2) { ……… ……… ……… }
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
31
Finally Block
The finally statement is associated with a try statement and identifies a block of statements that are executed regardless of whether or not an error occurs within the try block.
Defines the code that is executed always
If an exception arises with a matching catch block, then the finally block is executed after the catch block. If no exception arises, the finally block is executed after the try block.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
32
Try – catch - finally try
{ ……… ……… ……… } catch (exceptionType name) { ……… ……… ……… } finally { ……… ……… ……… } Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
33
Throws
The throws keyword is used along with the declaration of a method that can throw an exception.
The throws is used for handling method level exception rather than having more than one try catch block.
This makes it mandatory for anyone calling the method to have it in try block else the compiler will give an error.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
34
Custom Exceptions
Java provides numerous exception classes in its library.
The user also can define his/her own exception and throw it when the undesired condition occurs.
Use the throw clause to throw an exception
Throw statement is used explicitly to throw an exception.
Syntax: throw ThrowableInstance; Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
35
Throwing Exceptions public void read() throws IOException { // Some code that cause IO Exception throw new IOException(); } Calling a method in main(): Try { Read(); } catch(Exception e){} Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
36
Advantages of Exception Handling
Separates error handling code from regular code.
Groups error types and allows for error differentiation.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
37
Summary
Interfaces in Java
Packages in Java
Need and Advantages of using package
Creating a Package
Using Existing Packages
Some standard Library classes
Math, Util, string/stringbuffer
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
38
Summary
Exception Handling in Java
Errors and Exceptions
Try – Catch – Finally block
Throw & Throws
Some Built-in exceptions
User defined exceptions
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
39