Short on time? This page is the recap. For explanations and worked examples, use the full study guide.
Recap
- 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 variable of a reference type holds an object reference, which can be thought of as the memory address of that object.
- Common attributes and behaviors of related classes can be put in a superclass. Classes that extend it are subclasses; this is an inheritance relationship.
- All classes in Java are subclasses of the
Objectclass. - Designing and implementing inheritance relationships is outside the course and exam.
Key relationships
| Idea | Example |
|---|---|
| Class | Counter, String, Scanner |
| Object | one particular Counter; the text "banana" |
| Reference variable | Counter c; can refer to a Counter |
| Attribute | data stored in a variable (for Counter, count) |
| Behavior | a method (increment(), getCount()) |
| Copy a primitive | int y = x; copies the value; later changes to x do not affect y |
| Copy a reference | Counter b = a; copies the reference; a and b are the same object |
| Change via one name | visible through every other name for that object |
| Declaring only | Counter c; creates a variable, not an object |
| Variable type | Holds | Can be null |
|---|---|---|
int, double, boolean |
a value | no |
a class such as String or Counter |
an object reference | yes |
Assumptions and conventions
- In diagrams an arrow means “holds a reference to”. Two arrows to one box mean two names for one object.
- One class can have many objects, each with its own attribute values and the same behaviors.
- Objects are created with
newand a constructor (Topic 1.13). - Methods are called on objects through reference variables (Topic 1.14).
Mistakes to avoid
- Confusing the class with the object.
- Thinking a reference variable contains the object.
- Expecting
b = ato copy the object. - Being surprised that a change through one name shows through another.
- Treating
Counter c;as if it made aCounter. - Forgetting that all classes are subclasses of
Object. - Spending time on writing inheritance, which is outside the course.
Quick self-check
- What is the difference between a class and an object? (A class is the blueprint; an object is one instance created from it.)
- What does
String s;create? (A variable that can refer to aString; noStringobject yet.) - After
Counter b = a;how manyCounterobjects exist (ifareferred to one)? (Still one:aandbrefer to the same object.) - After
int y = x; x++;doesychange? (No.) - Which class is a superclass of every class? (
Object)
Next: practice questions.