AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/1/1-12)
Unit 1 · Topic 1.12
1.12 Objects: Instances of Classes
A class is a blueprint, and an object is one thing built from it. This topic explains that relationship, how classes can be arranged in a hierarchy, and why a variable of a class type holds a reference to an object rather than the object itself.
Key terms
- class
- object
- instance
- superclass
- subclass
- object reference
Classes and objects
A class is the formal description, or blueprint, of a kind of object. It says what attributes every object of that kind has and what behaviors it can perform.
An object is one specific instance of a class, with its own values for those attributes. Think of a class as the plans for a house and objects as the actual houses built from the plans. Every house has rooms and a front door, but each one has its own paint color and address.
So a Dog class might say every dog has a name and an age and can bark. One Dog object has the name Rex and the age 3; another has the name Luna and the age 7.
Class hierarchies and Object
Related classes often share attributes and behaviors. Java lets you put the shared parts in one general class, called a superclass, and build more specific classes from it, called subclasses. A subclass inherits (gets automatically) everything in its superclass without rewriting it, and can add its own parts. For example, a Vehicle superclass might have a speed and a move behavior, and its subclasses Car and Bicycle would both have them.
At the very top of every hierarchy is a class called Object. Every class in Java is a subclass of Object, directly or indirectly. That's why every object has an equals method and a toString method: they come from Object.
You won't be asked to design or write inheritance (classes that extend other classes) on the exam. You just need to know what a hierarchy is and that every class is an Object.
Reference variables
A variable of a primitive type, like int, holds the value itself. A variable of a reference type, like String or Counter, holds an object reference. You can think of it as the object's address in memory: it tells Java where to find the object, but it isn't the object.
This has a big consequence. If you copy a reference into another variable, both variables refer to the same object. They're called aliases. Changing the object through one variable changes what you see through the other, because there's only one object.
Picture it as arrows: c1 and c2 both point to the same Counter box in memory, while c3 points to a different box.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Two names, one object
A
Counterclass has aclick()method that adds 1 to its count and agetCount()method that returns the count. A newCounterstarts at 0. What does this code print?Counter c1 = new Counter(); Counter c2 = c1; Counter c3 = new Counter(); c2.click(); c2.click(); c3.click(); System.out.println(c1.getCount() + " " + c3.getCount());Show the solutionHide the solution
- Step 1: Line 1 creates one
Counterobject and stores a reference to it inc1. - Step 2: Line 2 doesn't create anything. It copies the reference, so
c2refers to the same object asc1. - Step 3: Line 3 creates a second, separate
Counterobject forc3. - Step 4: The two
c2.click()calls change the shared object, so its count is 2.c3.click()changes only the second object, so its count is 1. - Step 5:
c1.getCount()looks at the shared object, which says 2.
Answer: It prints
2 1.c1andc2are aliases for one object, so clicks made throughc2show up throughc1. - Step 1: Line 1 creates one
- Example 2
Class or object?
In a school app,
Studentdescribes what every student record stores (name, grade level, ID) and can do (report its GPA). The record for Jamal Reed, grade 11, is created when he enrolls. Which is the class and which is the object? What is the superclass ofStudentif no other is given?Show the solutionHide the solution
- Step 1:
Studentdescribes all students in general, with attributes and behaviors, so it is the class. - Step 2: Jamal's record has specific values for those attributes, so it's an object, an instance of
Student. - Step 3: Every class is a subclass of
Object, so with no other superclass named,Student's superclass isObject.
Answer:
Studentis the class; Jamal's record is an object (an instance) of it. Its superclass isObject. - Step 1:
Common mistakes
- Thinking
Counter c2 = c1;makes a copy of the object. It copies the reference, so both variables share one object. - Using "class" and "object" as if they mean the same thing. The class is the blueprint; objects are what you build from it.
- Thinking a reference variable holds the object's data directly. It holds a reference to where the object is.
On the exam
- Tracing questions often create aliases and then change the object through one name. Draw each variable as an arrow to its object, and count how many objects actually exist (one per
new).
Connected topics
Videos
Check yourself
3 questions on 1.12 Objects: Instances of Classes. Pick an answer to see if you got it, and why.
Which of the following best describes the relationship between a class and an object?
Consider the statement Scanner reader = new Scanner(new File("data.txt"));. Which of the following best describes what the variable reader holds?
A programmer writes a new class called Ticket and doesn't mention any other class in its header. Which of the following is true?
0 of 3 answered