AP Computer Science A · Study guide

Objects: Instances of Classes

Unit 1 · Using Objects and Methods · Topic 1.12

Objects: Instances of Classes

What separates a description from a thing, what a class hierarchy actually claims, and exactly where the course stops.

Exam-day recap
  • An object is a specific instance of a class with defined attributes; a class is the blueprint of attributes and behaviors.
  • A class is written once and instantiated as many times as needed; each new makes an object with its own attribute copies.
  • A class hierarchy puts common attributes and behaviors of related classes into one superclass.
  • Subclasses draw upon the superclass's attributes and behaviors without replacing them; this is reuse, not duplication.
  • The inheritance relationship runs from the subclasses to the superclass: a subclass knows what it extends; a superclass does not.
  • Designing and implementing inheritance relationships are outside the scope of the course and exam.
  • In scope is explaining a hierarchy; out of scope is designing or writing extends yourself.
  • All classes in Java are subclasses of the Object class.
  • Every class being a subclass of Object guarantees a toString exists, so concatenating any object into a String works.

1A Class Is a Description; an Object Is a Thing

Essential knowledge 1.12.A.1

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.

Two nouns, and the exam separates them on purpose. The class is written once and describes what every one of its objects will have. The object is a particular one of those things, in memory, with its own values filled in.

public class Service
{
   private String route;
   private String departureTime;
   private int platform;

   // ... constructor and behaviors
}

Nothing in that file is a train service. It is the description of one. No route has been chosen, no time set, no platform assigned — the attribute names exist and the attribute values do not.

The learning objective

1.12.A — explain the relationship between a class and an object. The verb is explain, and the three essential knowledge statements give you exactly three things to be able to explain: what an instance is, what a class hierarchy is, and where the Object class sits.

1.1 “With Defined Attributes” Is the Operative Phrase

The definition does not say an object is an instance of a class. It says an object is a specific instance of a class with defined attributes. That clause is what makes an object a thing you can ask questions of:

Service morning = new Service("Coastline", "07:12", 3);
Service evening = new Service("Coastline", "18:40", 1);

Both come from the same class. Both run the same route. They are still two different objects, because specific instance means this one, here, with these values — not this kind of thing.

A test that settles it every time

Can you ask it for a value? morning.getPlatform() answers 3, because morning is a specific instance with defined attributes. There is no question you can put to the class Service that has a platform for an answer, because the class does not have one to give.

2One Description, Many Instances

A class is written once and instantiated as many times as the program needs. Each new produces another object with its own copy of every attribute:

Service a = new Service("Coastline", "07:12", 3);
Service b = new Service("Valley Line", "07:25", 5);
Service c = new Service("Airport Express", "07:31", 2);

Three objects, one class. Changing b's platform changes nothing about a or c, and nothing about Service itself.

2.1 The Vocabulary, Used Precisely

WordWhat it names
ClassThe formal implementation — the blueprint of attributes and behaviors
ObjectA specific instance of that class, with its attribute values defined
InstanceThe same thing as an object, said from the class's point of view
InstantiateThe act of creating one — what new does
The sentence that loses the point

“A class is an object.” It is not, and the reverse is not true either. A class describes objects. Write “an object is an instance of a class” and the relationship is stated in the direction the course states it.

2.2 Why the Distinction Is Worth This Much Care

Every later topic depends on it. Instance variables belong to objects, which is why each object has its own. Class variables belong to the class, which is why there is only ever one. Constructors run when an object is created, not when a class is written. Every one of those sentences is unreadable until class and object are two different words in your head.

3Class Hierarchies — the Part You Explain

Essential knowledge 1.12.A.2

A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without replacing these in the code. This creates an inheritance relationship from the subclasses to the superclass.

Suppose the timetable also carries coach services. A coach has a route and a departure time just as a train does, but it leaves from a bay rather than a platform. Two classes would repeat most of themselves. A hierarchy puts what they share in one place:

// the superclass holds what both have in common
public class Service
{
   private String route;
   private String departureTime;
}

// the subclasses draw upon it and add what is their own
public class RailService extends Service  { private int platform; }
public class CoachService extends Service { private int bay; }

3.1 “Draw Upon … Without Replacing”

The essential knowledge chooses those words carefully. A subclass does not restate the superclass's attributes and behaviors in its own code. They are declared once, in the superclass, and the subclass inherits them. Every object still holds its own values: each RailService and each CoachService object has its own route and departureTime.

Read it as reuse, not duplication

If RailService and CoachService each declared their own route, that would be duplication and the hierarchy would have bought nothing. The point of lifting route into Service is that it is now written in exactly one place, and a correction to it is a correction for both.

3.2 Which Way the Relationship Runs

The last sentence of the essential knowledge states a direction: the inheritance relationship runs from the subclasses to the superclass. A subclass knows what it extends; a superclass does not know what extends it, and does not need to.

The direction is examinable

“Service inherits from RailService” is backwards. Say it the way the code reads: RailService extends Service — the subclass reaches up.

4The Exclusion Statement, Read Carefully

Exclusion statement, quoted

Designing and implementing inheritance relationships are outside the scope of the AP Computer Science A course and exam.

This is the most misread line in the unit, in both directions. Some students conclude inheritance is off the syllabus and skip it; others study it as though they will have to write a subclass. Neither is right, and the exclusion says exactly which half is which.

Excluded: designing a hierarchy, and implementing one. You will not be asked to decide what belongs in a superclass, and you will not be asked to write extends in a free-response answer.

Not excluded: everything in 1.12.A.2 itself. Explaining what a superclass is, what a subclass is, what “draw upon without replacing” means, and which way the relationship runs. The essential knowledge is required course content; the exclusion narrows what you do with it, not whether you know it.

A useful way to hold the line

The verb in the learning objective is explain. The verbs in the exclusion are design and implement. Anything you can answer in a sentence is in scope; anything that would require you to author a hierarchy is not.

5Every Class Sits Under Object

Essential knowledge 1.12.A.3

All classes in Java are subclasses of the Object class.

One sentence, and it is true of every class you have ever used and every class you will ever write. String is a subclass of Object. So is Service, without anybody writing extends anywhere — the relationship is automatic.

5.1 The Consequence the Course Actually Names

This is not trivia. A later essential knowledge statement, 1.15.A.5, leans on it directly: concatenating a String with any object implicitly calls that object's toString method — a behavior that is guaranteed to exist by the inheritance relationship every class has with the Object class.

System.out.println("Next: " + morning);   // calls morning.toString() implicitly

The reason that line compiles for any object, including one you wrote five minutes ago, is 1.12.A.3. Every class is a subclass of Object, so every object is guaranteed to have a toString.

What this does not license

Knowing that toString is inherited does not put overriding it in scope — the course excludes that separately. You are expected to know the method is always there and what a call to it produces, not to write your own version of it.

6How This Topic Is Assessed

6.1 Recognizing the Question

If the stem says…It is testing…
“instance of”, “created from”, “blueprint”1.12.A.1 — the class/object relationship
“superclass”, “subclass”, “extends”, “common attributes”1.12.A.2 — explaining a hierarchy, never designing one
“every class”, “guaranteed to exist”, an unfamiliar object printed1.12.A.3 — the Object root

6.2 A Model Explanation

A program has classes RailService and CoachService, both of which extend Service. Explain the relationship.

Too thin

“They inherit from Service.” True and worth almost nothing — it restates the code without saying what the relationship is or what it buys.

Scores

“Service is a superclass holding the attributes and behaviors the two kinds of service have in common, such as the route and the departure time. RailService and CoachService are subclasses that draw upon those without restating them, and each adds what is particular to it. The inheritance relationship runs from the subclasses to the superclass.”

Names the superclass, names what is common, uses “draw upon without replacing”, and states the direction — the essential knowledge satisfied rather than quoted.

7Check Yourself

A program creates four Service objects. How many classes are there?

Response

One. A class is written once and instantiated as many times as needed; four objects are four specific instances of that single blueprint, each with its own defined attributes.

Is Service a subclass of anything?

Response

Yes — of Object. All classes in Java are subclasses of the Object class, and nothing needs to be written to make that true.

A question asks you to design a class hierarchy for a school's vehicles. What should you notice?

Response

That it is outside the scope of the course. Designing and implementing inheritance relationships are excluded, so a real exam question will ask you to explain a hierarchy it gives you rather than to produce one.

Why does "Next: " + morning compile even though nobody wrote a toString for Service?

Response

Because Service is a subclass of Object, and that inheritance relationship guarantees a toString exists. Concatenating a string with any object implicitly calls it.

Two objects are created from the same class with identical arguments. Are they the same object?

Response

No. Each new produces a separate specific instance with its own attributes. Equal values do not make one object — they make two objects that happen to agree.

8Summary

IdeaWhat to remember
ClassThe formal implementation — the blueprint of attributes and behaviors
ObjectA specific instance of a class, with defined attributes
One to manyOne class, as many objects as the program instantiates
SuperclassHolds the attributes and behaviors related classes have in common
SubclassExtends a superclass and draws upon it without replacing it
DirectionThe inheritance relationship runs from the subclasses to the superclass
ExcludedDesigning and implementing inheritance — explaining it is not
The rootAll classes in Java are subclasses of Object
Why that mattersIt is what guarantees every object has a toString
The one sentence

A class is a description that exists once; an object is one specific thing made from that description, with its own values. Everything else in this topic — hierarchies, the Object root, the exclusion — is a consequence of taking that difference seriously.