CS110 Lecture 15 Thursday, March 25, 2004 • Announcements – hw6 due tonight – pass/fail, withdraw deadline April 8 • Agenda – – – – – Questions Better BankAccount (Inheritance) Overriding, class Object toString Trees Lecture 15 1 Banking System (version 5) • Several kinds of BankAccounts – – – – regular checking fee savings • Simulated time: every month – fee accounts are charged a fee – savings accounts accrue interest Lecture 15 2 Bank Application (version 5) Every class extends Object, directly or indirectly Lecture 15 3 class BankAccount • What all BankAccounts share – fields balance, transactionCount, issuingBank – methods withdraw, deposit, incrementBalance, countTransaction, getters and setters for fields • Changes from BankAccount.java (4): – abstract class (16): never new BankAccount() – constructor (31): invoked by children only – abstract method newMonth: (147) what each kind of BankAccount does on first day of month, implemented in each child class, called from Bank’s report and newMonth methods Lecture 15 4 class RegularAccount • Inherit from abstract BankAccount • Write as little as possible – take all the defaults • Constructor: public RegularAccount( int initialBalance, Bank issuingBank ) { super(initialBalance, issuingBank); } • implement abstract method newMonth public void newMonth() { } Lecture 15 5 class CheckingAccount • Behavior: like RegularAccount, but can write checks too. Checks cost money. • Design – inherit from BankAccount (not from RegularAccount!) – write empty newMonth method – write honorCheck method public int honorCheck( int amount ) { incrementBalance( - checkFee ); return withdraw( amount ); } Lecture 15 6 class FeeAccount • Behavior: like RegularAccount, but each transaction costs money, and there’s a monthly fee • Design – inherit from BankAccount – write newMonth method to charge monthly fee – override countTransaction method! subtract fee, then do whatever else is customary public void countTransaction() { incrementBalance( -transactionFee ); super.countTransaction(); } Lecture 15 7 class SavingsAccount • Behavior: – 5% annual interest credited monthly – only three free transactions per month, then each costs $1 • Design – inherit from BankAccount – write newMonth method to credit interest – figure out how to keep track of number of transactions in a month, override countTransaction method to charge fee when appropriate • Write it for hw7 Lecture 15 8 Where is the method? • Send aMessage to an object of class SomeClass • Java looks in SomeClass for method matching aMessage with the right signature • If not found, looks in parent class of SomeClass (there always is a parent, except for class Object) • If child and parent both have the method – child’s method overrides parent’s – if you really want the parent’s method, use super Lecture 15 9 Inheritance – two uses • Put features common to different kinds of objects in an abstract superclass, often with abstract methods • Make a (small) change in the behavior of some object by extending its class and overriding a method Lecture 15 10 toString • Suppose SomeClass foo = new SomeClass( ) • Then these two expressions do the same thing: System.out.println( foo.toString() ); System.out.println( foo ); • Every object knows how to respond to a toString message since there’s a toString in class Object • For “foo” etymology, see the full online dictionary of computer science at http://foldoc.doc.ic.ac.uk Lecture 15 11 class OverridingDemo • It’s often nice to override toString, to provide an informative String describing your particular kind of object • NamedObject overrides toString (71-74) • Create NamedObject instances named by command line arguments (33, 40) • println … 34 nobj.toString() 35 nobj itself 36 toString from class Object Lecture 15 implicit toString message weird 12 toString in class Object • NamedObject@206fdf64 • Not very informative • (class name)@(weird number) • weird number is actually base 16 (hexadecimal) (digits 0123456789abcde) • weird number may change when program runs again Lecture 15 13 toString in class Boolean • Wrapper class for primitive type boolean • From file Boolean.java in library: private boolean value; // field public String toString() { return value ? "true" : "false"; } • Sun’s brace convention differs from ours • test ? x : y expression on next slide Lecture 15 14 test ? x : y • Has value x if test is true, else has value y if (a > b) { max = a; } else { max = b; } same as max = ( a > b ) ? a : b; Lecture 15 15 toString for collections • TreeMapDemo.java 108 terminal.println(map.toString()); produces output {one=1, three=3, two=1} “{ (key.toString()=value.toString(), … }” • ArrayList toString produces “[ 0th item toString, 1st item toString … ]” • Very useful for debugging Lecture 15 16 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 form Lecture 15 17 Class hierarchy Note descriptive words root Lecture 15 18 File system organization • folder: place where Windows keeps information • For historical reasons, we use “directory” as a synonym for “folder” • A directory can contain – other directories (called subdirectories, subfolders) – files • Every directory is a subdirectory of its parent • The world starts at the only directory (on the disk drive) with no parent: the root directory, called “\” Lecture 15 19 root files are leaves Tree for cs110 web page Lecture 15 20