Beginner Reference Manual • Chapter 3

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:

🍪 Layman Analogy: Cookie Cutter vs Physical Cookies

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
                    
Architecture Specification 3.1: Variable Reference Pointer on the Stack pointing to Object Instance on the Heap
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:

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:

🧊 Layman Analogy: Family Refrigerator vs Personal Lunchbox

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

Exercise 3.1: Explain the Cookie Cutter analogy for Classes and Objects. Why can you create 100 objects from 1 class?
Step-by-Step Explanation for Beginners:
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!
Exercise 3.2: Differentiate between a Shared Static Variable and an Instance Variable.
Step-by-Step Explanation for Beginners:
- 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!