# AP® Computer Science A 1.12 Objects: Instances of Classes

Source: https://lockincs.com/ap-csa/unit-1/1-12-objects-instances-of-classes/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.12.A, 1.12.A.1, 1.12.A.2, 1.12.A.3, 1.12.B, 1.12.B.1

A class is the formal implementation, or blueprint, of the attributes and behaviors of an object, and an object is a specific instance of a class. Related classes can share a superclass, and every class in Java is a subclass of Object. A reference variable holds an object reference, which you can think of as the object's memory address.

## What you will learn

- 1.12.A Explain the relationship between a class and an object.
- 1.12.B Develop code to declare variables to store reference types.

## 1\. Classes and objects

A **class** is the formal implementation, or blueprint, of the attributes and behaviors of an object. An **object** is a specific instance of a class, with defined attributes 1.12.A.1.

One `Plant` class can produce many `Plant` objects. Each object has its own values for the attributes `name` and `height`:

```
Plant fern = new Plant("Fern", 30);
Plant mint = new Plant("Mint", 12);
fern.grow(4);
System.out.println(fern.getName() + " " + fern.getHeight());
System.out.println(mint.getName() + " " + mint.getHeight());
```

Output:

```
Fern 34
Mint 12
```

Growing `fern` does not change `mint`. They are two different objects built from the same blueprint.

## 2\. Class hierarchies: superclasses and subclasses

When related classes share attributes and behaviors, the shared parts can be written once, in one class called the **superclass**. This builds a **class hierarchy**. The classes that extend the superclass are its **subclasses**, and they can use the superclass’s attributes and behaviors without writing them again. The link from each subclass up to its superclass is an **inheritance** relationship 1.12.A.2.

Example: in a hierarchy with superclass `Vehicle` and subclasses `Bus` and `Bicycle`, both subclasses can use `Vehicle`’s attributes (such as `speed`) without rewriting them.

| Class | Role | What it has |
| --- | --- | --- |
| `Vehicle` | superclass | attributes and behaviors all vehicles share, such as `speed` |
| `Bus` | subclass of `Vehicle` | everything from `Vehicle`, plus its own, such as a number of seats |
| `Bicycle` | subclass of `Vehicle` | everything from `Vehicle`, plus its own, such as a number of gears |

Outside the course: designing and implementing inheritance relationships. You only need to understand what they are.

## 3\. Every class is a subclass of Object

All classes in Java are subclasses of the `Object` class 1.12.A.3. So every object, from any class, has the behaviors defined in `Object`, such as `toString` and `equals`, even if its own class never wrote them.

```
public class Coin
{
    // no methods written here at all
}

// in main
Coin c = new Coin();
System.out.println(c.equals(c));
```

Output:

```
true
```

`Coin` declares nothing, yet `c.equals(c)` compiles and runs, because `Coin` is a subclass of `Object` and draws on its `equals` behavior. Topic 1.15 uses the inherited `toString` behavior.

## 4\. Reference variables hold object references

A variable of a reference type holds an **object reference**, which can be thought of as the memory address of that object 1.12.B.1. Declare a reference variable with the class name as its type:

```
String s = "Hi";
String t = s;          // copies the reference, not the object
Plant a = new Plant("Basil", 10);
Plant b = a;           // b refers to the same Plant as a
b.grow(5);
System.out.println(t);
System.out.println(a.getHeight());
```

Output:

```
Hi
15
```

After `String t = s;`, `t` holds a reference to the same `String` object that `s` refers to. After `Plant b = a;`, there is still only one `Plant`, so growing it through `b` changes what `a.getHeight()` returns.

**Watch out:** a reference variable does not contain the object; it contains where to find it.

## Common mix-ups

- Not true: A class and an object are the same thing. The class is the blueprint. Objects are the specific things built from it, each with its own attribute values. One Plant class can produce many Plant objects.
- Not true: A reference variable contains the object. It contains an object reference, which you can think of as the memory address of the object. The object itself is stored elsewhere.
- Not true: A subclass must rewrite the attributes and behaviors it takes from its superclass. Subclasses draw on the existing attributes and behaviors of the superclass without replacing them in the code.

## In 30 seconds

- An object is a specific instance of a class with defined attributes.
- A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.
- A superclass holds attributes and behaviors common to related classes; subclasses extend it and draw on them without rewriting (inheritance).
- All classes in Java are subclasses of the Object class.
- A reference variable holds an object reference, like the memory address of the object.

## Checkpoint: 4 practice questions

### Question 1

`Bus` and `Bicycle` are subclasses of `Vehicle`. Which statement is true?

- A. `Vehicle` is an object created from `Bus`, which serves as its blueprint.
- B. `Bus` can use the attributes and behaviors of `Vehicle`, and every class is a subclass of `Object`.
- C. `Bus` must rewrite every attribute and behavior it takes from `Vehicle`.
- D. Only `Vehicle` extends `Object`; `Bus` and `Bicycle` do not.

**Answer: B.** Subclasses draw on the superclass's attributes and behaviors without rewriting them, and every class is a subclass of `Object`. A confuses classes with objects: all three are classes. C is wrong because subclasses do not replace the inherited code. D is wrong because all classes in Java are subclasses of `Object`.

### Question 2

After `String s = "Hi";` and `String t = s;`, what does `t` hold?

- A. A reference to the same `String` object that `s` refers to
- B. A separate copy of the `String` object
- C. The primitive characters H and i
- D. `null`

**Answer: A.** A reference variable holds a reference (like an address); `t = s` copies the reference, not the object. B is wrong because no new object is created. C is wrong because a String variable never holds primitive values. D is wrong because `s` refers to an object, so its reference is not null.

### Question 3

A `Song` class defines the attributes `title` and `length`. A program creates three `Song` objects. Which of the following is true?

- A. The program has created three `Song` classes.
- B. All three objects must have the same title.
- C. There is one `Song` class and three instances of it.
- D. Each `Song` object is a blueprint for the `Song` class.

**Answer: C.** A class is the blueprint; each object is a specific instance of it with its own attribute values. A confuses objects with classes. B is wrong because each object has its own attribute values. D reverses the relationship: the class is the blueprint for the objects.

### Question 4

**Plant class (implementations not shown)**

| Constructor or method | What it does |
| --- | --- |
| public Plant(String n) | Sets the name to n and the height to 0 |
| public Plant(String n, int h) | Sets the name to n and the height to h |
| public String getName() | Returns the name |
| public int getHeight() | Returns the height |
| public void grow(int cm) | Adds cm to the height |

Consider the following code segment. What is printed?

```java
Plant x = new Plant("Ivy", 20);
Plant y = new Plant("Ivy", 20);
Plant z = x;
z.grow(10);
System.out.println(x.getHeight() + " " + y.getHeight());
```

- A. `20 20`
- B. `30 30`
- C. `20 30`
- D. `30 20`

**Answer: D.** `z` holds the same object reference as `x`, so growing through `z` changes the object `x` refers to (30). `y` refers to a different object, which stays at 20. A treats `z` as a copy. B treats `x` and `y` as the same object because their values match. C mixes up which object changed.

## Questions students ask

**What is the difference between a class and an object in Java?**

A class is the blueprint that defines the attributes and behaviors. An object is a specific instance of that class, with its own attribute values. Many objects can be made from one class.

**What is a superclass and a subclass?**

A superclass holds attributes and behaviors that related classes have in common. Subclasses extend the superclass and can use those attributes and behaviors without rewriting them. This is an inheritance relationship.

**What is the Object class in Java?**

Object is the class at the top of every hierarchy: all classes in Java are subclasses of Object. That is why every object has methods such as toString.

**What does a reference variable store?**

An object reference, which you can think of as the memory address of the object. It does not store the object itself.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
