Download presentation
Presentation is loading. Please wait.
Published byDaisy Haynes Modified over 9 years ago
1
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes
2
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 2 Object behavior Bank account operations –deposit money –withdraw money –get the current balance Methods –deposit –withdraw –getBalance
3
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 3 Applying methods Transfer balance double amt = 500; momsSavings.withdraw(amt); harrysChecking.deposit(amt); Add interest double rate = 5; // 5% double amt = acct.getBalance() * rate / 100; acct.deposit(amt);
4
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 4 Constructing objects Construct an object new BankAccount() Save object in object variable BankAccount account = new BankAccount(); Apply methods account.deposit(1000);
5
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 5 Figure 1 Creating a New Object
6
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 6 Figure 2 Initializing an Object Variable
7
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 7 Class definition class BankAccount { public void deposit(double amount) { method implementation } public void withdraw(double amount) { method implementation } public double getBalance() { method implementation } data }
8
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 8 Method signatures access specifier (such as public ) return type (such as double or void ) method name (such as deposit ) list of parameters (such as double amount )
9
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 9 Instance Variables public class BankAccount {... private double balance; } access specifier (such as private ) type of variable (such as double ) name of variable (such as balance )
10
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 10 Figure 3 Instance Variables
11
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 11 Private data You can't access private data: harrysChecking.balance = 1000; // ERROR Use the public interface for all access: harrysChecking.deposit(1000); Hiding implementation = encapsulation Safe Makes it easy to change implementation
12
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 12 Implementing methods public class BankAccount { public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; } public double getBalance() { return balance; } private double balance; }
13
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 13 Implicit parameter public void withdraw(double amount) { balance = balance - amount; } balance is the balance of the object to the left of the dot: momsSavings.withdraw(500) means momsSavings.balance = momsSavings.balance - amount;
14
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 14 Constructors A constructor initializes the instance variables Constructor name = class name public class BankAccount { public BankAccount() { balance = 0; }... }
15
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 15 Multiple constructors public class BankAccount { public BankAccount(double initialBal) { balance = initialBal; }... } new BankAccount(5000)
16
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 16 Tips Missing quality tip: –Make all data private –Make most methods public Productivity hint: Keyboard shortcuts –Ctrl+C, Ctrl+V, Ctrl+X –Alt+Tab, Ctrl+Esc –Alt+letter accesses menu
17
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 17 Driver Program BankAccount class implements bank account. Need a separate class to do something with bank accounts public class BankAccountTest { public static void main (String[] args) { do something with bank accounts } }
18
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 18 Class BankAccount.java public class BankAccount { public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; } public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance - amount; }
19
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 19 public double getBalance() { return balance; } private double balance; }
20
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 20 Program BankAccountTest.java public class BankAccountTest { public static void main(String[] args) { BankAccount account = new BankAccount(10000); final double INTEREST_RATE = 5; double interest; // compute and add interest for one period
21
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 21 interest = account.getBalance() * INTEREST_RATE / 100; account.deposit(interest); System.out.println("Balance after year 1 is $" + account.getBalance()); // add interest again interest = account.getBalance() * INTEREST_RATE / 100; account.deposit(interest); System.out.println("Balance after year 2 is $" + account.getBalance()); }
22
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 22 Discovering Classes Nouns: candidates for classes Verbs: candidates for methods Example: Program asks a user to add coins to a purse. Then get total amount of money in the purse Classes: Coin, Purse Methods: addCoins, getTotal
23
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 23 Copying numbers double balance1 = 1000; double balance2 = balance1; balance2 = balance2 + 500; Change in balance2 does not affect balance1
24
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 24 Figure 5 Copying Numbers
25
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 25 Copying Object References BankAccount account1 = new BankAccount(1000); BankAccount account2 = account1; account2.deposit(500); Change through account2 is also visible through account1 Object variables hold references, not objects
26
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 26 Copying Object Ref- erences
27
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 27 Null Reference account1 = null; Now account1 refers to no account Can't call methods on null null is not the same as 0 Common error: A null string is not the same as the empty string ""
28
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 28 Figure 7 A null Reference
29
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 29 Figure 8 String References
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.