Unit 1 · 12 min
1.12 Objects: Instances of Classes
A class is the blueprint; objects are the things built from it.
Summary, objectives and course and exam description (CED) codes
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.
By the end of this session you can
- 1.12.A Explain the relationship between a class and an object.
- 1.12.B Develop code to declare variables to store reference types.
Reviewed against the CED on
1LearnThe notes, the traps and how to remember them
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.
Lock it in: memory hooks
- Class vs objectThe class is the cookie cutter; the objects are the cookies.
- ReferenceA reference variable holds the address, not the house.
Common misconceptions
"A class and an object are the same thing."
Not quite. 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.
"A reference variable contains the object."
Not quite. It contains an object reference, which you can think of as the memory address of the object. The object itself is stored elsewhere.
"A subclass must rewrite the attributes and behaviors it takes from its superclass."
Not quite. Subclasses draw on the existing attributes and behaviors of the superclass without replacing them in the code.
Exam traps
- A reference variable does not contain the object; it contains where to find it.
- Assigning one reference variable to another copies the reference, not the object: both then refer to the same object.
- Designing and implementing inheritance is outside the course, but you must know that every class is a subclass of Object.
2PracticeRecall it, then check yourself
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.
Cover this list, say the points out loud, then take the checkpoint without scrolling back up.
Checkpoint · 4 questions
Checkpoint: Objects: Instances of Classes
Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.
-
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
3PrevailLock it in for exam day
By the numbers
- Roles in a class hierarchy two roles CED 1.12.A.2
- superclassholds the common attributes and behaviors
- subclassextends the superclass and draws on them
Key terms: flip the cards
Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.
-
Classtap to flip
The formal implementation, or blueprint, of the attributes and behaviors of an object. A class defines a reference type.
Example and CED codes -
Instancetap to flip
An object created from a class. "An instance of Plant" means a Plant object.
Example and CED codes -
Superclass and subclasstap to flip
A superclass holds attributes and behaviors common to related classes; subclasses extend it and draw on them without rewriting.
Example and CED codes -
Inheritancetap to flip
The relationship from subclasses to a superclass: subclasses draw on the superclass's attributes and behaviors without replacing them.
Example and CED codes -
Object classtap to flip
The class at the top of every Java class hierarchy: all classes are subclasses of Object.
Example and CED codes -
Object referencetap to flip
The value a reference variable holds: it can be thought of as the memory address of the object.
Example and CED codes -
Reference typetap to flip
A data type used to define objects, that is, anything that is not a primitive type. Every class defines a reference type.
Example and CED codes -
Attributetap to flip
Data related to a class, stored in variables, such as a plant's name and height.
Example and CED codes -
Behaviortap to flip
What instances of a class can do, or what can be done with them, defined by methods, such as grow or getHeight.
Example and CED codes
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.