Download presentation
Presentation is loading. Please wait.
Published byClaude Little Modified over 8 years ago
1
Exercise 1 Review OOP tirgul No. 5 2006
2
Outline Polymorphism Exceptions A design example Summary
3
Inheritance structure Principles problem instanceOf problem
4
Inheritance (is-a relation) What if Zebra inherits from Bear ?
5
Inheritance structure(2) What happens if we add a function to UnaryExpression ?
6
Data Members
7
Data Members(2) Wrong context of code Memory waste Readability Modularity: –Adding trinary operator Shorter code is not always better !
8
instanceof breaks OOP Bad Style A heavy procedure Should be used only in extreme cases instanceof
9
public TreeNode next() { _stack.remouveFirst(); if(_current instanceof BinariNode) { _stack.addFirst(((BinariNode)_current).getRightSon()); TreeNode temp = _current; _current = ((BinariNode)_current).getLeftSon(); _stack.addFirst(_current); return temp; } if(_current instanceof UnariNode) { _stack.addFirst(((UnariNode)_current).getSon()); TreeNode temp = _current; _current = ((UnariNode)_current).getSon(); return temp; } … }
10
Why OOP principles? Why do we need to follow OOP principles? –Reusing ideas –Saving time –Correctness
11
Exceptions public static void main ( String args[] ) { try { activateProgram(); // controls the process flow } catch (IOException e) { System.err.println(“Error: “+e.getMessage()); System.exit(1) ; //error code 1 = bad termination } catch (MyException e) { e.printStackTrace() ; } // never leave a catch clause empty! }
12
Uncritical Errors - Recovery BufferedReader reader=new BufferedReader ( new InputStreamReader(System.in)); boolean isReadLine = false ; String line ; while (!isReadLine) { try { line = reader.readline() ; isReadLine = true ; } catch (IOException e) { System.err.println(“IO error!”) ; isReadLine = false ; }
13
Principles for exceptions Critical errors in a single place Recovery within the relevant place Throw and catch should not be in the same function Different exception classes for different types of errors Try to make it as general as you can
14
A Good Design Example Taken From Students Solutions
15
Expression Hierarchy
16
Summary Polymorphism and inheritance –is-a –Avoid instance-of whenever possible –Avoid unnecessary code –Maintain modularity Exception handling –Consider the contexts of usage
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.