Object-Oriented Thinking (Classes, Objects & Constructors)
Discover how software models real-world items using class blueprints, physical objects, state variables, actions (methods), constructors, and shared static fields.
In the real world, everything is an object: your phone, your car, your pet. Object-Oriented Programming (OOP) lets us create digital versions of real-world things! In this chapter, we will learn how to design blueprints (Classes) and create actual items (Objects) from them.
3.1. 1. Classes vs Objects: Blueprints & Physical Items
Definition: A Class is a digital blueprint specifying properties and actions; an Object is a physical instance created from that blueprint.
Core Working Principles:
- Classes are Blueprints: A class defines what an item will have (variables) and what it can do (methods).
- Objects are Physical Instances: You use the
newkeyword to build actual physical objects from a class. - Multiple Objects: You can build hundreds of unique individual objects from a single class blueprint!
Class (Blueprint): The metal Cookie Cutter. It is just the shape template—you cannot eat a metal cookie cutter!
Objects (Instances): The actual baked sugar cookies created using the cutter. You can press out 50 delicious individual cookies (Objects) from 1 single cookie cutter (Class)!
graph LR
subgraph Stack Memory Reference
RefPointer[Car myCar] -->|Points to Heap Memory Address| HeapMemory
end
subgraph Dynamic Heap Memory Region
HeapMemory["Car Object Instance
- brand: 'Toyota'
- speed: 60 mph"]
end
5-Marks Exam Scoring Points
- A Class is an abstract blueprint; an Object is a concrete instance in memory.
- Objects are instantiated dynamically using the 'new' keyword.
- Stack memory holds variable pointers; Heap memory holds actual object instances.
3.2. 2. Fields, Methods & Object Constructors
Definition: Fields store object properties, Methods perform object actions, and Constructors initialize new objects when they are created.
Core Working Principles:
- Fields (State): What an object knows (e.g.
brand,color,speed). - Methods (Behaviors): What an object does (e.g.
accelerate(),brake()). - Constructors: Special setup methods called automatically when an object is born with
newto give it initial values.
public class Car {
// Fields (Properties)
String brand;
int speed;
// Constructor (Setup method when object is born)
public Car(String carBrand, int startSpeed) {
brand = carBrand;
speed = startSpeed;
}
// Method (Action)
public void accelerate() {
speed += 10;
System.out.println(brand + " accelerated! Current speed: " + speed + " mph");
}
}
// Creating and using objects inside main
Car car1 = new Car("Toyota", 40);
car1.accelerate(); // Prints: Toyota accelerated! Current speed: 50 mph
5-Marks Exam Scoring Points
- Fields store properties; Methods define actions.
- Constructors initialize object fields during 'new' instantiation.
- The dot operator (car1.accelerate()) is used to access an object's methods and fields.
3.3. 3. Shared Class Features: The static Keyword
Definition: The static keyword marks variables or methods that are shared by the entire class as a whole, rather than belonging to individual objects.
Core Working Principles:
- Instance Variables: Unique copy for every single object (e.g., each Car has its own speed).
- Static Variables: Single shared box belonging to the whole class (e.g., total count of all cars manufactured).
- Accessing Static Members: Can be accessed directly using the Class Name (e.g.
Car.totalCarsCreated).
Instance Variable: Your personal lunchbox. Whatever food you put inside belongs exclusively to you.
Static Variable: The shared family refrigerator in the kitchen. Everyone in the house shares the exact same refrigerator!
5-Marks Exam Scoring Points
- static members belong to the class itself and are shared by all instances.
- Instance variables are unique per object instance.
- Static methods can be called directly without creating an object instance.
✍️ Chapter 3 Reference Exercises & Review Problems
1. Class as Template: A Class is simply an architectural design template (like a metal cookie cutter). It consumes no memory for object data itself.
2. Objects as Instances: An Object is a physical piece of dough stamped out by that template.
3. Reusability: Because the template defines the rules once, you can stamp out 100 or 1,000 distinct objects in computer memory using `new` without altering the template!
- Instance Variable: Stored inside each specific object on the Heap. Changing `car1.speed` does NOT affect `car2.speed`.
- Static Variable: Stored once in the shared Class memory area. Changing `Car.totalCount` updates the value universally for all objects across the application!