Unit 1.12: Objects — Instances of Classes¶
Scope: CS Awesome 2, Section 1.12
Learning Goals¶
By the end of this lesson, you should be able to:
- Distinguish a class from an object.
- Identify attributes and behaviors.
- Explain what an object reference represents.
- Recognize superclass and subclass relationships.
- Model a real entity as an object.
Classes and Objects¶
A class defines a reference type. It is a blueprint describing the attributes and behaviors shared by objects of that type.
An object is a specific instance of a class.
| Class | Example objects |
|---|---|
String |
"north", "status" |
Scanner |
a scanner connected to keyboard input |
Turtle |
individual drawing turtles with different positions |
The class is the general definition; each object has its own identity and state.
Attributes and Behaviors¶
An attribute, also called an instance variable or field, is data an object knows about itself. A behavior is an action implemented by a method.
For a delivery robot:
| Attributes | Behaviors |
|---|---|
| identifier | move forward |
| battery level | turn |
| location | report status |
| direction | recharge |
Two robot objects can have the same available behaviors but different current battery levels and locations.
Reference Variables¶
A variable of a class type stores an object reference, which can be thought of as information locating an object in memory.
String first = "signal";
String second = first;
Both variables now refer to the same String object. Copying a reference does not automatically create a second object.
first ──┐
├──> String object "signal"
second ──┘
A reference variable may also hold null, meaning it currently refers to no object.
String message = null;
Creating an Object: Preview¶
Objects are commonly created with new and a constructor call. Constructors are the focus of Section 1.13.
Scanner input = new Scanner(System.in);
Read this from right to left:
new Scanner(System.in)creates aScannerobject.inputis a reference variable of typeScanner.- the resulting reference is stored in
input.
Declaration alone does not create an object:
Scanner input;
Class Hierarchies and Inheritance¶
Related classes can be organized in a class hierarchy. A superclass contains common attributes and behaviors. A subclass extends a superclass and inherits accessible behavior.
Object
└── Vehicle
├── Bicycle
└── Bus
In this model, Bicycle and Bus are subclasses of Vehicle. Shared behavior such as move() can be defined in Vehicle.
Every Java class ultimately extends the Object class. Designing inheritance structures is beyond the AP CSA implementation scope, but recognizing the relationship is important.
Objects in a Program¶
String route = "Blue Line";
System.out.println(route.length());
route is a reference variable, "Blue Line" is a String object created from a literal, and length() is one of the object's behaviors.
The dot operator asks the object referenced by route to perform a method.
Practice Missions¶
Mission 1: Class or Object?¶
Classify each item and justify your answer:
Scanner;- the scanner referenced by
input; String;- the string object
"Emergency"; Turtle;- a turtle named
atlasat coordinate(40, 90).
Mission 2: Model a Smart Locker¶
Design a conceptual SmartLocker class. List at least four attributes and four behaviors. Then describe two different locker objects and give their differing state.
Mission 3: Reference Diagram¶
Draw a reference diagram after each statement:
String alpha = "ready";
String beta = alpha;
String gamma = null;
gamma = beta;
Count the variables, references, and distinct string objects.
Mission 4: Build a Hierarchy¶
Organize Device, Phone, Laptop, and Object into a hierarchy. Add one shared attribute and one shared behavior at the most appropriate level. Explain what the subclasses inherit.
Key Summary¶
| Concept | Core idea |
|---|---|
| Class | Blueprint and reference-type definition. |
| Object | A specific instance of a class. |
| Attribute | Data describing object state. |
| Behavior | Action implemented by a method. |
| Reference | Value locating or identifying an object. |
| Superclass | General class containing shared features. |
| Subclass | More specific class that inherits features. |
Source scope: CS Awesome 2, Unit 1.12