Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 – Data Structures Lecture 10: More Inheritance & Exceptions.

Similar presentations


Presentation on theme: "CSC 212 – Data Structures Lecture 10: More Inheritance & Exceptions."— Presentation transcript:

1 CSC 212 – Data Structures Lecture 10: More Inheritance & Exceptions

2 Method Inheritance Subclass overloads methods like normal Subclass can also override method  Identical signature, no less restrictive access  Which method called depends on instance Use of super keyword within a method  Usage of super different within constructor  Calls method as defined in superclass  Only used in overriding method definition  Call is super.methodName(…);

3 Field Inheritance Non-private fields inherited as normal  Private fields accessible through methods in superclass Fields are hidden not overriden  Declare field with same name in subclass  Types do not need to the same  Objects have both field

4 Accessing Hidden Field Field accessed depends on variable type  Use field that corresponds to type of variable  Superclass methods & variables of superclass type use field defined in superclass  Subclass methods can access field from superclass using super.fieldName  Subclass methods & variables of subclass type use field defined in subclass

5 Inheritance Review Non-private methods & fields inherited by subclass  As if they were cut-and-pasted into subclass Method called depends on instance type Field used depends on variable type Sorry it is this ugly (There is a good reason for this; if curious ask me outside of class)

6 Error Handling Goals Best way to handle error that prevents executing as normal?  Must return to where this error can be fixed  Handle error in manner that makes sense for that moment of time  If class is reused, may want to handle error differently  Guarantee error is handled or known by user

7 Solve Problems with Exceptions ex-cep-tion: n. an instance or case not conforming to the general rule  Proper way to signal error in object-oriented code Java already includes many exceptions  ArrayIndexOutOfBoundsException signals access to nonexistent array entry  NullPointerException generated when using null reference

8 Exceptions in Java Throw exception when problem detected Handle problem by catching exception In Java, exceptions are actually objects  Must first be instantiated (new)  Can assign exception object’s fields  Then throw the exception instance

9 Throwing an Exception private float withdrawal(float amt) throws BadRequestException { if (amt < 0) { BadRequestException bre=new BadReqestException(); bre.setRequest(amt); throw bre; } else if (amt > balance) { throw new BadRequestException(amt, balance); } balance -= amt; return balance; }

10 Handling Exceptions When an exception is thrown, methods can do one of 3 things:  handle the error and continue  handle the exception and throw a new one  ignore the exception Once thrown, exception must be handled or program must end  Want air traffic using computer after it lost a plane?

11 Catching an Exception Blocks can list multiple catch statements  Exceptions will be caught by first matching catch statement  If no catch matches current exception, exception is thrown on to calling method try-catch blocks do not need to catch all possible exceptions  But uncaught exceptions are passed on

12 Catching an Exception public void atmWithdrawal(float amt) { try { withdrawal(amt); addToRegister(amt); } catch (BadRequestException bre) { if (bre.getAmount() < 0) { System.err.println(“Trying to get rich?”); } else { System.err.println(“Get a job, deadbeat!”); } } catch (Exception e) { System.out.println(“Caught other exception”); System.err.println(e.toString()); } }

13 Catching an Exception (2) public void withdrawalWithGun(float amt) throws BadRequestException { callPolice(); addDyePacks(); withdrawal(amt); }

14 Checked vs. Unchecked Two different flavors of exceptions Checked exceptions MUST be listed in throws clauses  Listing required if thrown in method  Also needed if the method “throws” the exception by ignore it Unchecked exceptions can be thrown anywhere and need not be declared  Used fixing error is impossible

15 Checked vs. Unchecked Exception Subclasses of Exception (except RuntimeException) are checked exceptions. These represent errors a programmer must fix. Runtime Exception Subclasses of RunTimeException are unchecked exceptions. These represent errors that occur during runtime and may not be able to be fixed.

16 Tracing With Exceptions public static void dumbExample(int i) { int j = 0; char c = ‘a’ try { System.out.println(“Starting”); if (i == 0) { throw new DumbException(); } System.out.println(“Ending”); } catch (DumbException de) { System.out.println(“Excepting”); } }

17 Another Tracing Example public static int generatesException() throws TraceException { try { System.out.println(“Starting gE”); throw new TraceException(); System.out.println(“Ending gE”); return 0; } catch (NullPointerException npe) { return 1; } } public static void callsException(boolean callIt) { try { System.out.println(“Starting cE”); if (callIt) { generateException(); } System.out.println(“Ending cE”); } catch (TraceException te) { System.out.println(“Caught te”); } }

18 public static int generatesException() throws TraceException { try { System.out.println(“Starting gE”); throw new TraceException(); System.out.println(“Ending gE”); return 0; } catch (NullPointerException npe) { return 1; } } public static void callsException(boolean callIt) { try { System.out.println(“Starting cE”); if (callIt) { generateException(); } System.out.println(“Ending cE”); } catch (TraceException te) { System.out.println(“Caught te”); } } public static void masterMethod() { callsException(false); callsException(true); }

19 Your Turn Get back into groups and do activity

20 Before Next Lecture… Review week #4 assignment Continue programming assignment #1  It is due 2 weeks from last Friday Start reviewing for Midterm #1  It will be 2 weeks from last Monday Friday’s lecture discusses object hierarchies & generic types  Read Sections of the book


Download ppt "CSC 212 – Data Structures Lecture 10: More Inheritance & Exceptions."

Similar presentations


Ads by Google