Skip to main content

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.

  1. Example 1

    Two names, one object

    A Counter class has a click() method that adds 1 to its count and a getCount() method that returns the count. A new Counter starts 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 solution
    1. Step 1: Line 1 creates one Counter object and stores a reference to it in c1.
    2. Step 2: Line 2 doesn't create anything. It copies the reference, so c2 refers to the same object as c1.
    3. Step 3: Line 3 creates a second, separate Counter object for c3.
    4. 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.
    5. Step 5: c1.getCount() looks at the shared object, which says 2.

    Answer: It prints 2 1. c1 and c2 are aliases for one object, so clicks made through c2 show up through c1.

  2. Example 2

    Class or object?

    In a school app, Student describes 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 of Student if no other is given?

    Show the solution
    1. Step 1: Student describes all students in general, with attributes and behaviors, so it is the class.
    2. Step 2: Jamal's record has specific values for those attributes, so it's an object, an instance of Student.
    3. Step 3: Every class is a subclass of Object, so with no other superclass named, Student's superclass is Object.

    Answer: Student is the class; Jamal's record is an object (an instance) of it. Its superclass is Object.

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.

Question 1 of 3

Which of the following best describes the relationship between a class and an object?

Question 2 of 3

Consider the statement Scanner reader = new Scanner(new File("data.txt"));. Which of the following best describes what the variable reader holds?

Question 3 of 3

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