Download presentation
Presentation is loading. Please wait.
Published byKatrina Walters Modified over 9 years ago
1
“The task of leadership is not to put greatness into people, but to elicit it, for the greatness is there already.” – John Buchan Thought for the Day
2
Reading from Files Use a FileReader BufferedReader in = new BufferedReader(new FileReader("f.txt")); String s; int total = 0; while ((s = in.readLine()) != null) { total += Integer.parseInt(s); } in.close(); System.out.println("Total = " + total);
3
Writing to Disk Files Use a FileWriter Methods to write strings and characters
4
Going Further Much more to I/O in Java –Binary data files –Random access files –Reading and writing objects (serialization) –Network I/O Even database access (JDBC)
6
Chapter 11 More Classes and Objects
7
Important Object-Oriented Concepts Inheritance Method overriding Polymorphism
8
Inheritance Often classes (or objects) have a lot in common –e.g. bank accounts Account balance accountNumber interestRate deposit, withdraw
9
Inheritance (cont.) Other types of accounts will be similar –e.g. cheque (current) account ChequeAccount balance, accountNumber interestRate overdraftLimit, overdraftIntRate deposit, withdraw
10
Problems Duplication of code and data Difficult maintenance –e.g. account number format changes More types of accounts –even more duplication
11
Inheritance — The Solution Classes can inherit properties from other classes –Like genetic inheritance In Java: public class X extends Y { } // class X In Python we would write: class X(Y):...
12
Inheritance (cont.) Diagrammatically: X... Y...
13
Inheritance (cont.) Cheque account class: public class ChequeAccount extends Account { public double overdraftLimit; public double overdraftIntRate; } // class ChequeAccount This class inherits: –balance, account number and interest rate –withdraw and deposit methods
14
Some Terminology Superclass (parent class) Subclass (child class) Subclassing (inheriting) In Java: only one superclass –single inheritance Y XA
15
Another Example Throwable ExceptionError RuntimeException IOException...
16
Overriding Methods Sometimes a subclass needs to do things a little differently to the superclass –e.g. withdrawals from a cheque account Method overriding –replaces inherited method –it is not the same as overloading
17
Overriding Methods (cont.) public class ChequeAccount extends Account { public double overdraftLimit; public double overdraftIntRate; public void withdraw (double amount) {... // Allow for overdraft } // withdraw } // class ChequeAccount
18
The Object Class Superclass of all Java classes public class X { } // class X extends Object
19
Object Methods All Java objects inherit some common methods from the Object class –toString –equals
20
The toString Method By default gives class name and memory address: ChequeAccount@3fbdb0 But, we can override it! ChequeAccount cq = new ChequeAccount(); System.out.println(cq);
21
Overriding toString public class ChequeAccount extends Account {... public String toString () { return (accountNumber + ” bal = R” + balance); } // toString } // class ChequeAccount
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.