Learn how to design class blueprints and instantiate them into functional objects.
Imagine you are an architect designing a futuristic city. Instead of drawing every single brick for 1,000 identical houses, what if you could design one master 'blueprint' and simply click a button to spawn as many unique houses as you need?
In Object-Oriented Programming (OOP), a Class is a formal blueprint or template. It doesn't 'exist' as a piece of data you can manipulate directly; rather, it defines the structure. A class specifies two things: Attributes (what the object knows, like or ) and Methods (what the object does, like ). Think of a class like a recipe for a cake. The recipe isn't the cake itself—you can't eat it—but it dictates exactly how every cake made from it will look and taste.
Quick Check
If a 'Smartphone' is a class, what would 'battery_level' be considered?
Answer
An attribute (or property).
To actually use a class, we must perform Instantiation. This is the process of creating a specific Object (also called an Instance) from the class blueprint. When you instantiate a class, the computer allocates a block of memory to hold that specific object's data. While the class is singular, you can create number of instances. Each instance is a distinct entity; changing the color of 'Car A' does not automatically change the color of 'Car B', even though they both came from the same `Car` class.
1. Define the class: `class Car { String color; }` 2. Instantiate the first object: `Car myCar = new Car();` 3. Set its state: `myCar.color = "Red";` 4. Instantiate a second object: `Car yourCar = new Car();` 5. Set its state: `yourCar.color = "Blue";`
Result: We have two distinct objects in memory, where .
Quick Check
True or False: You can only create one object from a single class definition.
Answer
False
The State of an object is the collection of values currently stored in its attributes. Even if two objects have the exact same state (e.g., two identical white iPhones), they have different Identities. In memory, they exist at different addresses. This is crucial for advanced programming: you might have a list of 100 'Enemy' objects. Even if they all have , they are individual targets that must be tracked separately by the CPU.
Imagine a game with a `Warrior` class. 1. Create `warrior1` and `warrior2`. 2. Both start with . 3. `warrior1` takes damage: . 4. Now, `warrior1.HP` is , but `warrior2.HP` remains .
This demonstrates that while they share the same logic (the class), they maintain independent data (the instance).
When we say `Car myCar = new Car()`, the variable `myCar` is actually a Reference. It doesn't 'hold' the object; it holds the memory address where the object lives. If you set `Car anotherCar = myCar;`, you haven't created a new car! You've created a second pointer to the same object. This is a common source of bugs in complex systems where multiple parts of a program might try to modify the same instance simultaneously.
1. `User userA = new User("Alice");` 2. `User userB = userA;` (Both point to the same memory block). 3. `userB.name = "Bob";` 4. Question: What is `userA.name`? 5. Answer: It is now "Bob", because and are aliases for the same instance. To create a separate person, you must use the `new` keyword again.
Which of these best describes the relationship between a Class and an Object?
If you have a class `Dog` and you execute `Dog d1 = new Dog(); Dog d2 = new Dog();`, how many instances exist?
Changing an attribute value in one object instance will automatically update that same attribute in all other instances of the same class.
Review Tomorrow
In 24 hours, try to explain the difference between a 'Class' and an 'Instance' to someone who doesn't code using a non-car analogy.
Practice Activity
Write a class definition for a 'BankAccount' with attributes for 'balance' and 'ownerName'. Mentally trace what happens if you create two different accounts.