Lecture 21
Lecture 21
Classes
suggested reading:
Java Ch. 6
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others.
Plan for today
•Recap: HashMaps + What’s Trending
•Classes
•Recap
2
Recap: HashMaps
3
Plan for today
•Recap: HashMaps + What’s Trending
•Classes
•Recap
4
Large Java Programs
There are some large programs written in Java!
5
Defining New Variable Types
Inbox Database Email Sender Login Manager
6
What Is A Class?
A class defines a
new variable type.
7
Why Is This Useful?
• A student registration system needs to store info about
students, but Java has no Student variable type.
• A music synthesizer app might want to store information
about different types of instruments, but Java has no
Instrument variable type.
• An email program might have many emails that need to be
stored, but Java has no Email variable type.
• Classes let you define new types of variables, which lets you
decompose your program code across different files.
8
Classes Are Like Blueprints
iPod blueprint (class)
state:
current song
volume
battery life
behavior:
power on/off
change station/song
change volume
choose random song
constructs
9
What Is A Class?
A class defines a
new variable type.
10
Creating A New Class
Let’s define a new variable type called BankAccount
that represents information about a single person’s
bank account.
A BankAccount:
- contains the name of account holder
- contains the balance
- can deposit money
- can withdraw money
11
What if…
What if we could write a program like this:
12
Creating A New Class
1. What information is inside this new
variable type? These are its private instance
variables.
13
Example: BankAccount
// In file BankAccount.java
public class BankAccount {
// Step 1: the data inside a BankAccount
private String name;
private double balance;
}
14
Creating A New Class
1. What information is inside this new
variable type? These are its instance
variables.
2. What can this new variable type do?
These are its public methods.
15
What if…
What if we could write a program like this:
account.setName(“Nick”);
String accountName = account.getName();
...
public void setName(String newName) {
if (newName.length() > 0) {
name = newName;
}
}
21
Constructors
GRect rect = new GRect();
22
Constructors
BankAccount ba1 = new BankAccount();
23
Example: BankAccount
public class BankAccount {
// Step 1: the data inside a BankAccount
private String name;
private double balance;
public ClassName(parameters) {
statements;
}
27
What Is A Class?
A class defines a
new variable type.
28
Creating A New Class
1. What information is inside this new
variable type? These are its instance
variables.
2. What can this new variable type do?
These are its public methods.
3. How do you create a variable of this type?
This is the constructor.
29
Recap
•Recap: HashMaps + What’s Trending
•Classes
•Recap