14.ppt

advertisement
CS110 Lecture 14
Tuesday, March 23, 2004
• Announcements
– hw6 due Thursday
– pass/fail, withdraw deadline April 8
• Agenda
–
–
–
–
–
Questions
Java 1.5
Inheritance (Chapter 5)
Shapes
JFile, TextFile and Directory
Lecture 14
1
Java version 1.5
• 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
Lecture 14
2
Backward compatibility
• Important software design issue
• Java 1.5 has no new keywords
• To compile java 1.4 source code with 1.5
– javac Whatever.java
• 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?
Lecture 14
3
TreeMap (1.5) summary
• 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( )
read as “in”
• looping: for( KeyType key : mapName.keySet( ))
{ // do whatever with key }
Lecture 14
4
New for syntax works with arrays too
• zamples.com/JspExplorer/samples/
samplesJDK1_5.html
int array[] = {1, 2, 3, 4};
int sum = 0;
for (int e : array)
sum += e;
System.out.println(sum);
• But note how this code violates our conventions
– we require int[] array
– braces should surround one line for loop body
Lecture 14
5
Design problem
• 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?
Lecture 14
6
Design problem
• 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?
Lecture 14
7
Design problem
• 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
Lecture 14
8
Why do we need a better way?
• 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)
Lecture 14
9
Inheritance
class Line
abstract class Line: fields and
methods for lines (length,
getLength, setLength)
class HLine:
paintOn(Screen,int,int)
class VLine:
paintOn(Screen,int,int)
parent class,
superclass
class HLine extends Line
child classes,
subclasses
class VLine extends Line
Lecture 14
10
How inheritance works
(Hline.java main)
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
Lecture 14
11
How inheritance works
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
Lecture 14
12
abstract
• New Java keyword
• abstract class can’t be instantiated
(only its children can)
• abstract method is a signature with no body
(just a promise)
Lecture 14
13
Constructors in a subclass
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”)
Lecture 14
14
Shapes in hw7
class Object
abstract class Shape: fields and methods needed by all child
classes (paintChar, getter and setter, paintOn(Screen))
abstract class Line: fields and
methods for lines (length,
getLength, setLength)
abstract class Rectangle fields and methods for
rectangles (length, width)
class HLine:
paintOn(Screen,int,int)
class Box
class VLine:
paintOn(Screen,int,int)
class Frame
class Shape // extends Object
class XLine extends Shape
class HLine extends XLine
Lecture 14
15
Trees
• 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
Lecture 14
16
Download