Welcome to Object-Oriented Development: Understanding Objects
Welcome to one of the most fundamental topics in CCEA AS Level Software Systems Development! In this chapter, we are exploring Objects. If you have ever wondered why this programming style is called Object-Oriented Programming (OOP), you are in the right place. Objects are the heart and soul of OOP.
Don't worry if programming terminology has felt a bit abstract up to now. By using everyday analogies and clear step-by-step breakdowns, these notes will help you master the concept of objects, how they are created, and how they interact in a running program.
1. What Exactly is an Object?
In the real world, look around you: your phone, your water bottle, a car driving past, or your pet are all objects. In software development, an object is a self-contained programming unit that bundles together data and the operations that act on that data.
Every object in OOP has three core characteristics:
• State: What the object knows or contains (its current values or data).
• Behaviour: What the object does (its actions or capabilities).
• Identity: What makes the object unique from all other objects, even if they have the exact same state.
Real-World Analogy: A Bank Account
• State (Attributes): Account number (e.g., 10023456), Account holder name (e.g., "Sarah Connor"), Current balance (e.g., £250.50).
• Behaviour (Methods): Deposit(), Withdraw(), CheckBalance().
• Identity: Even if two bank accounts belong to people with the same name and identical balances, they are two separate, unique account objects with different locations in memory.
Did you know? In software systems, treating software components as objects makes complex systems much easier to design, debug, and reuse across different applications.
Key Takeaway: An object combines state (data/fields) and behaviour (methods/functions) into a single, identifiable entity.
2. The Relationship Between Classes and Objects
One of the most common mistakes in CCEA AS examinations is confusing a Class with an Object. Let's make sure you never mix them up!
The Blueprint Analogy
Think of an architectural blueprint for a house:
• The blueprint is the Class: It is just a drawing on paper. It defines where the walls, windows, and doors will be, but nobody can live inside a blueprint.
• The actual physical house built on the street is the Object: You can live in it, paint the walls a specific colour, and open its front door.
• From a single blueprint (class), you can construct fifty houses (objects). Each house can have a different front door colour or different furniture inside, but they all share the same structural design.
Other Useful Analogies:
• Cookie Cutter vs. Cookie: The cookie cutter is the Class; the baked cookies are the Objects.
• Car Factory Spec Sheet vs. The Car: The specification document is the Class; the shiny vehicle on your driveway is the Object (an instance of the class).
Memory Aid: The "C-O-I" Rule
Always remember: A Class is a template. An Object is an Instance of that class. The process of creating an object from a class is called Instantiation.
Key Takeaway: A class is the conceptual plan (the code you write); an object is the living, working instance created in the computer's memory when your program runs.
3. Anatomy of an Object: State and Behaviour
Let's look closer at how state and behaviour are represented in code.
1. State (Attributes / Fields / Properties)
An object's state is stored in variables (often called fields or attributes) defined within the class. Over time, an object's state can change.
Example: If a player character takes damage in a game, its healthPoints variable might drop from \(100\) to \(85\). Its state has changed!
2. Behaviour (Methods / Operations)
An object's behaviour is defined by the methods (subroutines or functions) written in the class. Methods allow an object to manipulate its own state or interact with other objects.
Example: Calling the method TakeDamage(15) updates the internal health value.
Exam Example Walkthrough: A Car Object
Consider a Car class:
• Fields (State): make, model, speed, fuelLevel.
• Methods (Behaviour): Accelerate(), Brake(), Refuel().
When the program runs, you might instantiate two car objects:
• car1: State = { make: "Ford", model: "Focus", speed: 0, fuelLevel: 50 }
• car2: State = { make: "Tesla", model: "Model 3", speed: 60, fuelLevel: 90 }
Both objects share the exact same behaviours (Accelerate, Brake), but hold their own independent state.
Key Takeaway: State is represented by data values stored in variables, while behaviour is represented by callable methods that execute code.
4. Object Instantiation: How Objects are Created
How does an object actually come into existence in your code? Let's break down the declaration and instantiation process step-by-step.
Step-by-Step Breakdown
In languages like C#, creating an object usually takes the following form:
Car myCar = new Car("Ford", "Focus");
Here is what happens under the hood:
1. Declaration (Car myCar): You create an object reference variable called myCar of type Car. At this exact moment, no actual Car object exists yet; the variable is ready to hold a reference.
2. The new Keyword: This keyword tells the computer's memory manager: "Allocate new memory space on the Heap for a Car object!"
3. Constructor Invocation (Car("Ford", "Focus")): A special method called a Constructor executes immediately to initialise the object's fields with starting values.
4. Assignment (=): The memory address of the newly created object is assigned to the reference variable myCar.
Memory Insight: Stack vs. Heap
Don't worry if memory management feels complex; here is a simple way to picture it:
• The Stack: Holds the reference variable (the address tag / name tag).
• The Heap: Holds the actual object data (the big bundle of values and state).
The reference on the stack points directly to the object located in the heap.
Key Takeaway: The new keyword requests memory space and calls the constructor to initialise a fresh object instance.
5. Object References and Identity
Understanding how reference variables work is essential for writing error-free software and scoring top marks in CCEA exam questions.
Reference vs. Value
Primitive types (like int or bool) hold their actual value directly. Objects, however, are reference types. A variable for an object holds the address of the object, not the object itself.
What Happens with Assignment?
Consider this code snippet:
Car carA = new Car("Red");
Car carB = carA;
What just happened here? Did we create two red cars? No!
We only used the new keyword once, so there is only one Car object on the heap. Both carA and carB are pointing to the very same object in memory!
If you execute: carB.SetColour("Blue");
Then checking carA.GetColour() will now return "Blue" because both variables point to that exact same instance.
The Concept of null
If an object reference variable has been declared but has not been assigned to any instance, it holds the value null. Attempting to call a method or access an attribute on a null reference causes a NullReferenceException (a runtime crash).
Key Takeaway: Object variables store references (pointers). Assigning one object variable to another copies the pointer, not the object itself.
6. Common Pitfalls and How to Avoid Them
• Pitfall 1: Confusing Class with Object in exam explanations.
Fix: Always check your wording. If you are discussing the definition, use the word Class. If you are discussing an active instance with real data at runtime, use the word Object.
• Pitfall 2: Forgetting the new keyword.
Fix: Writing Student s1; only creates an empty reference. You must write Student s1 = new Student(); to bring the object to life.
• Pitfall 3: Assuming two objects with identical values are the same object.
Fix: Two separate instances with the same values occupy different memory locations; they have distinct identities.
7. Quick Review Summary
Let's review the main points from this chapter:
• Object: An instance of a class that encapsulates state (data) and behaviour (methods).
• Class: The blueprint or template from which objects are instantiated.
• State: The values stored inside the fields/attributes of an object at any given time.
• Behaviour: The actions an object can perform via its methods.
• Instantiation: Using the new keyword and a constructor to allocate memory and create an object.
• Reference: A variable that stores the memory address of an object rather than holding the data directly.