Loading…
Loading…
Charting your lesson…
Opening your lesson…
Classes & Objects
Lesson 5 of 5
A class is a blueprint; an object is an instance of it with its own state, created via a constructor.
6 steps•about 11 min•8 flashcards
Step 1/ 61 min
Everything in OOP starts here: a class is the blueprint, an object is the thing built from it. Interviewers use this "basic" topic to check that your mental model — instances, state, constructors, static vs instance — is actually solid.
Step 2/ 62 min
A class is a blueprint (a template/type) that defines what data (fields/attributes) an object holds and what it can do (methods/behavior). An object is a concrete instance of a class, created at runtime, with its own copy of the field values (its state). One class, many objects: the Car class defines wheels and drive(); each actual car is a separate object with its own color and speed.
You create an object by instantiating the class (often with the "new" keyword), which invokes a constructor — a special method that initializes the object's fields. Each object has its own state (field values) and shares the class's behavior (methods). A reference variable points to the object in memory.
Objects also often need lifecycle handling: constructors to set up, and (in some languages) destructors/finalizers or explicit close() to clean up resources.
Two objects can be equal in value yet have different identity (different memory references). Languages distinguish reference equality (same object) from value equality (same contents), which you customize via equals()/hashCode() or operator overloads. Objects typically live on the heap, with references on the stack; unreferenced objects are reclaimed by garbage collection (or manual deletion). A class may also define encapsulated private state and expose a clean public interface — tying classes back to the other OOP pillars.
Step 3/ 61 min
Define a class Car with fields color and speed and a method accelerate(). Create two objects: myCar = new Car("red") and yourCar = new Car("blue"). Calling myCar.accelerate() changes only myCar's speed — the objects have independent state. A static field totalCars, incremented in the constructor, is shared: after two instantiations it reads 2 for the whole class, not per object.
Step 4/ 61 min
Classes and objects are the fundamental unit of modeling in OOP: represent each real-world or domain concept (User, Order, Invoice) as a class, and create objects to represent specific instances. Use static members for data/behavior that belongs to the type as a whole rather than any instance.
Step 5/ 61 min
Remember "class = cookie cutter, object = cookie." One cutter (class) stamps out many cookies (objects), each with its own toppings (state) but the same shape (behavior).
Don't confuse the class with its objects, or instance members with static ones. The class is the definition (loaded once); each object is a separate instance with its own state. A static member is shared by all instances — changing it affects every object's view of it.
Step 6/ 6· Worked examples1 min
Problem
What does this print, and what is the difference between the two fields?
class Counter {
static int total = 0; // class-level (shared)
int id; // instance-level (per object)
Counter() { total++; id = total; } // constructor
}
Counter a = new Counter();
Counter b = new Counter();
print(a.id, b.id, Counter.total);
Try it on paper first.
Recall
Keep this
Fast revision of classes and objects.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.