Excepaons dealing with the unexpected sources of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ehavioral contract that the parent methods assume about overridden methods (and the child follows that contract). –  Either way: document the design. –  Use the @Override annotaAon to mark intenAonal overriding •  Look for other means of achieving the desired outcome: –  Use composiAon & delegaAon (i.e. wrapper objects) rather than overriding. The final modifier •  By default, fields and local variables are mutable and methods can be overridden*. •  The final modifier changes that. •  Final fields and local variables: –  Must be iniAalized (either by a staAc iniAalizer or in the constructor) and cannot thereajer be modified. –  Act like the immutable name bindings in OCaml –  static final fields are useful for defining constants (e.g. Math.PI) •  Final methods cannot be overridden in subclasses. –  Also useful in combinaAon with static! –  Prevents subclasses from changing the “behavioral contract” between methods by overriding. *Technically, fields can also be re- declared in a subclass (i.e. C has field x and D extends C and also declares a field x, not even necessarily of the same type!). Don’t do this! But be aware that you can introduce bugs by inadvertently using this “feature”. ExcepAons Dealing with the unexpected. Sources of method Failure •  Some methods may require that their arguments saAsfy certain precondiAons –  Input to max is a nonempty list, Item is non- null, no more elements for next •  Interfaces may be imprecise –  Some Iterators don't support the "remove" operaAon •  External components might fail –  Try to open a file that doesn't exist •  Resources might be exhausted –  Program uses all of the computer's disk space •  These are all excep5onal circumstances… –  how do we deal with them? CIS120 / Spring 2012 Ways to handle failure •  Return an error value (or default value) –  –  –  – ...
View Full Document

This document was uploaded on 11/24/2013.

Ask a homework question - tutors are online