(61B Sp25) Lecture 2 - Defining and Using Classes
(61B Sp25) Lecture 2 - Defining and Using Classes
● Any time I’m live coding, I advise you to pause frequently and try to anticipate my next
move. You’ll probably learn more by trying to guess what I’m going to do rather than just
watching me do it.
Lecture 2
Bark!
Coding Demo: Dog class
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
public static void makeNoise() {
System.out.println("Bark!");
}
}
public static void main(String[] args) {
makeNoise();
}
}
Bark!
Coding Demo: Dog class
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
public static void makeNoise() { public static void main(String[] args) {
System.out.println("Bark!"); Dog.makeNoise();
} }
}
Bark!
Dog
We could create a separate class for every single dog out there, but this is going to get
redundant in a hurry.
Classes can contain not just functions (a.k.a. methods), but also data.
● For example, we might add a size variable to each Dog.
These instances are
also called ‘objects’
Classes can be instantiated as objects.
● We’ll create a single Dog class, and then create instances of this Dog.
● The class provides a blueprint that all Dog objects will follow.
○ For the example above, all Dog objects will have a size.
Let’s try this out. See the webcast video or the following hidden slides for the step-by-step
process of constructing the Dog class.
Coding Demo: Dog class
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
public static void main(String[] args) {
Dog.makeNoise();
} }
} }
Coding Demo: Dog class
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
int weightInPounds; public static void main(String[] args) {
Dog.makeNoise();
} }
} }
Coding Demo: Dog class
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
int weightInPounds; public static void main(String[] args) {
Dog.makeNoise();
bark.
Coding Demo: Dog class
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
int weightInPounds; public static void main(String[] args) {
Dog d = new Dog();
d.weightInPounds = 51;
d.makeNoise();
woooooof!
Coding Demo: Dog class
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
int weightInPounds; public static void main(String[] args) {
Dog d = new Dog(51);
public Dog(int w) { d.makeNoise();
weightInPounds = w;
}
woooooof!
Our Dog Class
public class Dog { For those of you who know Python, the
public int weightInPounds; equivalent code is given below.
Classes can contain not just functions (a.k.a. methods), but also data.
● For example, we might add a size variable to each Dog.
These instances are
Classes can be instantiated as objects. also called ‘objects’
● We’ll create a single Dog class, and then create instances of this Dog.
● The class provides a blueprint that all Dog objects will follow.
○ For the example above, all Dog objects will have a size.
○ Cannot add new instance variables to a Dog. They must ALL obey the blueprint
exactly.
Example:
0 1
Classes in Java
• Defining and Instantiating Classes
• Class Terminology
• Static vs. Instance Members
Lists, Arrays, and Maps
• Lists in Java 4.0
• Abstract Data Types vs. Concrete
Static vs. Instance •
Implementations
Modern Java Lists
Members •
•
Arrays
Maps
Lecture 2, CS61B, Spring 2023 Summary
Static vs. Non-Static
Non-static
Sometimes, classes may have a mix of static and non-static methods, e.g.
}
} }
Coding Demo: maxDog
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
int weight; public static void main(String[] args) {
public Dog(int w) { ... } Dog d = new Dog(15);
public void makeNoise() { ... } Dog d2 = new Dog(100);
}
} }
Coding Demo: maxDog
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
int weight; public static void main(String[] args) {
public Dog(int w) { ... } Dog d = new Dog(15);
public void makeNoise() { ... } Dog d2 = new Dog(100);
}
} }
Coding Demo: maxDog
Dog.java DogLauncher.java
public class Dog { public class DogLauncher {
int weight; public static void main(String[] args) {
public Dog(int w) { ... } Dog d = new Dog(15);
public void makeNoise() { ... } Dog d2 = new Dog(100);
int i = 0;
while (i < manyDogs.length) {
Dog.maxDog(manyDogs[i], mediumDog).makeNoise();
i = i + 1;
}
}
}
Answer to Question
Lists support a variety of operations which vary according to the whims of the authors who
wrote the code for the list. Some examples:
● Append an item, e.g. we could append 18 to the list above.
● Retrieve an item by index, e.g. we could ask for the 0th item and get 3.
● Removing an item by index or value, e.g. we could remove the 9 from the list.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
L = [] import java.util.List;
L.append("a")
L.append("b") public class ListDemo {
L.append("c") public static void main(String[] args) {
print(L) List L = new List();
}
['a', 'b', 'c'] }
Lists in Java Attempt #3
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
L.append("c") public class ListDemo {
print(L) public static void main(String[] args) {
List L = new ArrayList();
['a', 'b', 'c'] }
}
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
L.append("c") public class ListDemo {
print(L) public static void main(String[] args) {
List L = new ArrayList();
['a', 'b', 'c'] L.add("a");
L.add("b");
L.add("c");
System.out.println(L);
Classes in Java
• Defining and Instantiating Classes
• Class Terminology
• Static vs. Instance Members
Abstract Data Lists, Arrays, and Maps
Types vs. •
•
Lists in Java 4.0
Abstract Data Types vs. Concrete
Implementations
Concrete • Modern Java Lists
• Arrays
Implementations • Maps
Summary
Lecture 2, CS61B, Spring 2023
Alternate Types of List
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
L.append("c") public class ListDemo {
print(L) public static void main(String[] args) {
List x = new ArrayList();
['a', 'b', 'c'] ...
}
}
Alternate Types of List
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
L.append("c") public class ListDemo {
print(L) public static void main(String[] args) {
List x = new LinkedList();
['a', 'b', 'c'] ...
}
}
List
In Python, there is no distinction between the abstract idea of a list and an actual list.
● A list is a list is a list. Programmer has no choice of what type of list.
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
L.append("c") public class ListDemo {
print(L) public static void main(String[] args) {
List x = new LinkedList();
['a', 'b', 'c'] ...
}
Abstract Data Types vs. Concrete Implementations
List
Linked CopyOnWrite
... ... ArrayList
List ArrayList
Abstract Data Types
List
Linked CopyOnWrite
... ... ArrayList
List ArrayList
Abstract Data Types
We’ll come to explore this concept in much more detail next week. For now, let’s get back to
modernizing our Java code.
List
Linked CopyOnWrite
... ... ArrayList
List ArrayList
Classes in Java
• Defining and Instantiating Classes
• Class Terminology
• Static vs. Instance Members
Lists, Arrays, and Maps
• Lists in Java 4.0
• Abstract Data Types vs. Concrete
Implementations
• Modern Java Lists
Modern Java Lists •
•
Arrays
Maps
Lecture 2, CS61B, Spring 2023 Summary
Retrieving from Lists
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
print(L[0]) public class ListDemo {
public static void main(String[] args) {
a List L = new ArrayList();
L.add("a");
L.add("b");
}
}
Retrieving from Lists
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
print(L[0]) public class ListDemo {
public static void main(String[] args) {
a List L = new ArrayList();
L.add("a");
L.add("b");
System.out.println(L.get(0));
}
Java 4.0 List Limitation
The list we created is a Java 4.0 style list (from Java before 2005).
● Serious limitation: Can’t (easily) assign the results of retrieving an item from a list to a
variable.
● I won’t teach you the Java 4.0 way to deal with this, because it is obsolete.
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
x = L[0] public class ListDemo {
public static void main(String[] args) {
Won’t compile. Results in a List L = new ArrayList();
“Required type: String, L.add("a");
Provided: Object” error.
L.add("b");
String x = L.get(0);
}
Java 5.0 Lists
In 2005, a new angle bracket syntax was introduced to deal with this issue.
● Instead of List L, the programmer specifies the specific type of list with List<String> L.
● Requires <> on the instantiation side.
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append("b")
x = L[0] public class ListDemo {
public static void main(String[] args) {
List<String> L = new ArrayList<>();
L.add("a");
Historical note: You had to have
<String> on both declaration L.add("b");
and instantiation sides until String x = L.get(0);
2011. }
Java 5.0 Lists
All items in a list created using bracket notation must have the same type!
● Unlike Python lists which can store multiple types.
L = [] import java.util.List;
L.append("a") import java.util.ArrayList;
L.append(3)
# fine public class ListDemo {
public static void main(String[] args) {
List<String> L = new ArrayList<>();
L.add("a");
Syntax error. Won’t compile.
L.add(3);
}
}
Limitations
Going from Java 4.0 to Java 5.0 lists meant losing the ability to have lists with multiple
types.
Going from Java 4.0 to Java 5.0 lists meant losing the ability to have lists with multiple
types.
Java has a special collection called an “array” that is a restricted version of the list ADT.
● Size that must be declared at the time the array is created.
● Size cannot change.
● All items must be of the same type.
● No methods.
● Syntax for accessing array entries is similar to Python, e.g. x[0].
Why do you think Java has both lists and arrays (basically lists with fewer features)?
Question for You (My Answers)
Why do you think Java favors arrays over lists (has special bracket syntax, doesn’t require
imports)?
Question for You (My Answer)
Why do you think Java favors arrays over lists (has special bracket syntax, doesn’t require
imports)?
● Java is a language built for performance.
● By contrast, Python is built to be beautiful/simple/elegant.
Below, we see a simple example where we create a dictionary (a.k.a. map) in Python.
● Let’s try to do the same thing in IntelliJ.
m = {}
m["cat"] = "meow"
m["dog"] = "woof"
sound = m["cat"]
Maps in Python
import java.util.Map;
import java.util.TreeMap;
Starting next week, we’ll be digging into the distinction between LinkedLists and ArrayLists.
● Will take about 3 lectures to understand.
Will more fully discuss TreeMaps and HashMaps in roughly week 7 of the course.
List Map
Linked
ArrayList TreeMap HashMap
List
Todo!