1
|
- Announcements
- hw6 due Thursday
- pass/fail, withdraw deadline April
8
- Agenda
- Questions
- Java 1.5
- Inheritance (Chapter 5)
- Shapes
- JFile, TextFile and Directory
|
2
|
- Beta release available from Sun
- download now for your PC if you wish
- no midsemester change to lab computers
- Improved collection management
- Casts no longer required
- Automatic wrapping of primitive types
- New for loop syntax replaces Iterator idiom
- See java 1.5 link on course home page
- TreeMap15Demo.java available there
|
3
|
- Important software design issue
- Java 1.5 has no new keywords
- To compile java 1.4 source code with 1.5
- To compile java 1.5 features
- javac –source 1.5 Whatever.java
- java 1.4 code still compiles (but with warnings)
- What should JOI do?
|
4
|
- declaration:
TreeMap<KeyType, ValueType> mapName;
- creation: new TreeMap<KeyType,
ValueType> ( );
- put:
mapName.put(KeyType key, ValueType val)
- get: mapName.get( KeyType key)
- no cast
to ValueType needed
- length: mapName.size( )
- looping: for( KeyType key : mapName.keySet( )) { // do whatever with key }
|
5
|
- zamples.com/JspExplorer/samples/
samplesJDK1_5.html
- But note how this code violates our conventions
- we require int[] array
- braces should surround one line for loop body
|
6
|
- Bigbank and Littlebank are similar but different
- Both have fields
- name, balance, transaction count (same meaning)
- accountList (different meanings)
- Both have methods
- visit, processTransaction, getBalance, … (same code)
- whichAccount, report (different code)
- Can we write big and little Bank classes without copying code?
|
7
|
- Directory should store TextFiles and Directories
- Directory and TextFile both have
- owner, create/mod date (same meaning)
- size, contents (different meanings)
- Directory has methods to add to, get from and loop on its contents (the
TreeMap of files in it)
- TextFile has methods to manipulate its text
- Can we write these classes without copying code?
|
8
|
- HLine and VLine each have length, paintChar, with setters and getters
- Each kind of line has a paintOn method to paint itself on a Screen in
its own particular way
- When you wrote VLine you started with a copy of HLine, and changed a
small fraction of the code
- Is there a better way?
- Yes
|
9
|
- Copy code and change a little bit Ž design problem
- can’t see similarities and differences
- bugs must be fixed in two places
- Inheritance solves this problem
- Inheritance is the second Big Thing in OOP (sending messages is the
first Big Thing)
|
10
|
|
11
|
- 64 Line hline = new HLine( 10,
‘x’);
- Variable hline declared of type Line can have a value that’s a reference
to an instance of some child of Line
- Line foo = new Line() not legal (since Line is abstract)
- 70 hline.setPaintChar(‘*’);
- Send setPaintChar message to Line hline, which happens
to be an HLine
- Java looks for setPaintChar method in class Hline
- Not there! So looks in parent class Line
- Found it! So search stops
|
12
|
- 65 hline.paintOn(screen);
- Send paintOn message to Line hline, which happens
to be an HLine
- No paintOn(Screen) in class HLine
- Java finds paintOn(Screen) in parent class Line
- paintOn(Screen) delegates to paintOn(Screen, int, int)
- paintOn(Screen, int, int) is abstract in class Line
- Java finds implementation in class HLine
|
13
|
- New Java keyword
- abstract class can’t be instantiated (only its children
can)
- abstract method is a signature with no body (just a promise)
|
14
|
- 64 Line hline = new HLine( 10, ‘x’);
- invokes HLine constructor
- 20 public HLine( int length, char
paintChar )
- 21 {
- 22 super( length, paintChar );
- 23 }
- line 22: invokes parent class (Line) constructor (java keyword super is “my
parent”)
|
15
|
|
16
|
- Common in computer science:
- Java class hierarchy (shows inheritance)
- Windows tree for files and directories (folders)
- Vocabulary: Tree, hierarchy
- Root (often drawn at the top!)
- Child, parent, branch, leaf, node
- Draw with arrows,
or in outline (indented) form
|