Objects: Instances of Classes
| English | Chinese | Pinyin |
|---|---|---|
| class | 类 | lèi |
| object | 对象 | duì xiàng |
| blueprint | 蓝图 | lán tú |
| instance | 实例 | shí lì |
Classes and objects
- A class 类 is a blueprint 蓝图 — it describes what a kind of thing has and can do.
- An object 对象 is a concrete instance 实例 built from that blueprint.
- One
Stringclass; many String objects ("cat","dog","Ada"). - The class defines the pattern; each object is one filled-in copy.
Blueprint vs. instance
- Think cookie cutter (class) vs. cookies (objects).
- The cutter shapes them; each cookie is a separate, real thing.
- Every object has its own data, even though they share the class's design.
- Changing one object doesn't change the others.
State and behavior
- An object bundles state (its data — instance variables) and behavior (its methods).
- A
Stringobject's state is its characters; its behavior includeslength(),substring(). - You interact with an object by calling its methods.
- This bundling of data + methods is the heart of object-oriented programming.
Class or object?
A class is the blueprint; an object is a specific instance. Sort each.
Objects have reference types
- An object is stored via a reference — the variable holds its address, not the object.
String s = "hi";—srefers to a String object somewhere in memory.- Two references can point to the same object.
- Reference types behave differently from primitives — a theme of the next lessons.
A class is a type/blueprint; an object is one instance of it — don't conflate them. String is the class; "cat" is an object. Each object carries its own state, so two String objects are independent. And an object variable holds a reference (an address), not the object's bytes directly — this matters when you copy or compare them later.
The class/object relationship:
- Class:
String(the blueprint — every String can dolength(),substring(), …). - Objects:
"cat","dog"— two separate instances, each with its own characters. "cat".length()is3;"dog".length()is3— same behavior, independent state.
A class is a blueprint describing state (instance variables) and behavior (methods); an object is a concrete instance of a class. Many objects can share one class's design while each holds its own state. Objects are reference types — a variable stores the object's address, not the object itself.
The relationship between a class and an object is like...
A class is the blueprint; an object is an instance built from it.
A specific object built from a class is called an ___ of that class (one word).
Objects are instances of a class.
Two objects of the same class each have their own independent state.
They share the design but hold separate data.
An object variable actually stores...
Objects are reference types — the variable holds an address.
In 'String s = "cat";', String is the ___ and "cat" is the ___.
String is the class (blueprint); "cat" is an object (instance).