Chapter: Objects in Object-Oriented Development

Welcome to one of the most fundamental chapters in CCEA AS Level Software Systems Development! In this chapter, we are going to demystify what an object actually is. If you have ever felt confused by programming terms, do not worry—object-oriented development is simply a way of designing software that mirrors how we see things in the real world.

By the end of these notes, you will understand what makes an object unique, how objects are created from classes, and how they interact with each other in software systems.

---

1. What is an Object?

In Object-Oriented Programming (OOP), an object is a self-contained component that combines data and functionality. It is a concrete instance of a class.

To understand objects, it helps to first understand their relationship with a class:

The Cookie Cutter Analogy:
The Class: Think of a class as a cookie cutter. The cookie cutter is just a metal shape (a blueprint/template). You cannot eat the cookie cutter!
The Object: The cookie cutter is pressed into dough to create actual, edible cookies. Each individual cookie is an object (an instance) created from that single blueprint.

The Architectural Blueprint Analogy:
The Class: An architect's floor plan for a three-bedroom house.
The Object: The actual physical houses built on a street using that plan. You can build 50 houses from one plan, and each house can have different wall colours or furniture, but they all share the same underlying structure.

Key Takeaway: A class is the blueprint, while an object is the living, working entity created from that blueprint.

---

2. The Three Pillars of an Object: State, Behaviour, and Identity

Every object in software has three defining characteristics. A great way to remember this is the mnemonic SBI: State, Behaviour, Identity.

A. State (What the object knows / contains)

The state of an object refers to the data stored inside it at any given moment. This data is held in attributes (also known as fields, instance variables, or properties).

Real-world example: For a Car object, its state might include:
make: "Ford"
model: "Fiesta"
currentSpeed: 30
fuelLevel: 75%

As the car drives or refuels, its state changes, but it remains the same car.

B. Behaviour (What the object can do)

The behaviour defines the actions or operations that an object can perform. These are written as methods inside the class.

Real-world example: For our Car object, its behaviours might include:
accelerate() (increases currentSpeed)
brake() (decreases currentSpeed)
refuel() (increases fuelLevel)

C. Identity (What makes the object unique)

The identity is a unique handle that distinguishes one object from every other object in the computer's memory, even if two objects happen to have identical state values.

Real-world example: Imagine two identical silver Ford Fiestas fresh off the factory line. They have the exact same make, model, colour, and engine size (identical state), but they are still two separate cars with different vehicle identification numbers (unique identity).

Quick Review:
State: Data values (Variables / Attributes)
Behaviour: Actions (Methods / Subroutines)
Identity: Memory location / Reference

---

3. Object Instantiation: Bringing Objects to Life

The process of creating an object from a class is called instantiation. When an object is instantiated, memory is allocated to hold its specific state.

The Two Steps of Object Creation

Creating an object typically involves two parts:

1. Declaration: Telling the compiler what type of object you want to store and naming the reference variable.
Example: Car myCar;
(At this stage, no car object has been built yet; the variable myCar points to nothing, which is represented as null).

2. Instantiation and Initialisation: Using the new keyword and calling a constructor.
Example: myCar = new Car("Ford", "Fiesta");
The new keyword allocates memory, and the constructor method sets the initial state of the object.

These two steps are frequently combined into a single line of code:
Car myCar = new Car("Ford", "Fiesta");

Did You Know?

When you create an object, the variable (myCar) does not actually hold the full object data. Instead, it holds an object reference (a memory address) that points to where the object lives in the system memory (the Heap).

Key Takeaway: An object is instantiated using the new keyword, which triggers a constructor to set up the object's initial values in memory.

---

4. Interacting with Objects

Once an object exists, how do we get it to do work? We use the dot operator (.) to interact with it.

A. Calling Methods (Message Passing)

In OOP, asking an object to perform an action is often referred to as passing a message. We do this by calling its methods:

myCar.accelerate();
myCar.brake();

B. Accessing Properties

Depending on accessibility (encapsulation), we can inspect or update attributes directly or via getter/setter methods:
int speed = myCar.getSpeed();

Multiple Objects Interacting

In a complete software system, programs work by having multiple objects send messages to one another. For example, a Driver object might call the startEngine() method on a Car object, which in turn calls the injectFuel() method on an Engine object.

Key Takeaway: Objects communicate and perform tasks via method calls using the dot operator.

---

5. Object References vs Object Values

This is a topic where many students lose easy marks in exams, so let's break it down carefully!

Reference Assignment

Look at the following sequence:

Car carA = new Car("Red");
Car carB = carA;

What happened here? Did we create two separate red cars?
No! We created one car object in memory, and now both carA and carB are pointing to the exact same object.

If you change the colour using carB.setColour("Blue");, then carA.getColour() will also return "Blue" because both references point to the same physical memory space.

Null References

If a reference variable is declared but not assigned to an object, its value is null (it points to nowhere in memory). Attempting to call a method on a null reference results in a Null Reference Exception (a common run-time crash).

Key Takeaway: Assigning one object variable to another copies the reference (memory pointer), not the object itself.

---

6. Common Mistakes to Avoid

Confusing Class and Object: Remember that a class is the template (code written in your IDE), while an object is the dynamic data structure created in memory while the program runs.

Forgetting the 'new' keyword: Writing Car myCar; only creates an empty variable. You cannot call methods on it until you instantiate it with new Car().

Assuming identical values mean the same object: Two objects can have identical attributes (e.g., both are red cars), but they have different memory identities.

---

7. Chapter Summary: Quick Revision Checklist

Make sure you can confidently explain the following points for your exam:

1. Object: An instance of a class containing state and behaviour.
2. State: The current data values held in an object's attributes.
3. Behaviour: The actions an object can perform via its methods.
4. Identity: The unique memory reference that distinguishes an object from all others.
5. Instantiation: Creating an object using the new keyword and a constructor.
6. Dot Operator (.): The syntax used to call methods or access attributes on an object.
7. Reference Variable: A variable that stores the memory address of an object, rather than the object's raw data directly.