ICS 22
Klefstad
Week 3
Inheritance and Polymorphism
Interfaces, Exceptions, File I/O

Hierarchy, Abstraction, and Inheritance

Class Relationships

Subclasses and Inheritance

class Object

Overriding Methods

Dynamic Binding of Methods

Explicit Casting

Data Hiding and Encapsulation

Abstract Classes

Polymorphism

Extended Example


Extended Example






Interfaces

Extending Interfaces

Exceptions and Handlers

Predefined Exceptions

Catching and Throwing Exceptions
class MyException extends Exception { String message; MyException( String s ) { super(s); } } class ExceptionsExample { static void println(String s) { System.out.println(s); } void f( int i ) { println("Entering f"); if ( i % 2 == 0 ) // i is even throw new MyException("I is even"); else throw new MyException("I is odd"); println("Leaving f"); } void testExceptions( int value ) { println("Entering testExceptions"); f( value ); println("Leaving testExceptions"); } public static void main(String [] args) { for ( int i=0; i<10; i++ ) try { println("going to call testExceptions"); testExceptions( i ); println("back from call to testExceptions"); } catch ( MyException e ) { prinln(e.toString()); } } }
Exception Example Output
going to call testExceptions Entering testExceptions Entering f I is even going to call testExceptions Entering testExceptions Entering f I is odd going to call testExceptions Entering testExceptions Entering f I is even going to call testExceptions Entering testExceptions Entering f I is odd
File Input and Output