Object-Oriented and Additional Programming: Classes, Objects, and Instantiation

Hello! Welcome to one of the most exciting and fundamental concepts in modern computer science: Object-Oriented Programming (OOP). Don't worry if the terminology seems heavy at first; we're going to break it down using real-world examples.

In this chapter, we learn how to structure our code around "things" (objects) rather than just a sequence of actions. This approach helps us build large, complex programs that are easier to manage, debug, and reuse. Mastering classes and objects is essential for A-Level programming success!

Why Object-Oriented Programming (OOP)?

The core reason for using the Object-Oriented Paradigm is to model real-world systems more accurately in code. It helps manage complexity in large programs by providing structure and organization.

Advantages of using OOP:

  • Modularity: Programs are built from self-contained units (objects), making code easier to test and maintain.
  • Reusability: Once a class is defined, you can create many objects from it without rewriting the definition.
  • Abstraction: You only interact with what you need, hiding the complex internal workings.
  • Scalability: It's easier to add new features or expand the program by creating new classes that interact with existing ones.


1. Classes: The Blueprint

Imagine you want to build 10 different racing cars for a simulation game. You wouldn't draw the complete design 10 times, would you? You create one master plan or template.

In OOP, this master plan is the Class.

A Class is a definition or a blueprint that describes the characteristics (data) and behaviours (actions) that all objects of that type will possess. It is not the object itself, but the structure used to create the object.

Key Components Defined by a Class

A class brings together data and the functions that operate on that data:

1. Property or Attribute:
These are the data items that define the characteristics of an object. They are like variables contained within the class definition.

  • Analogy: If the class is a Car, the properties might be Colour, MaxSpeed, and EngineSize.

2. Method:
These are the functions or subroutines defined within a class that describe the actions or behaviours an object can perform, or the actions that can be performed upon it.

  • Analogy: If the class is a Car, the methods might be Drive(), Brake(), or HonkHorn().

Quick Review: Class Structure

A Class = (Data/Properties) + (Functions/Methods)


2. Objects: The Real Thing

If a Class is the blueprint for a car, an Object is the actual car sitting on the road.

An Object is an instance of a class. It is a specific entity created based on the class blueprint. Each object will have its own unique set of values for the properties defined in the class.

  • Example: If we have a Car class, we can create two objects:

Object A (Car 1):
Property: Colour = Blue
Property: MaxSpeed = 200
Method: Drive() (This method works just for this blue car).

Object B (Car 2):
Property: Colour = Red
Property: MaxSpeed = 180
Method: Drive() (This method works just for this red car).


Did you know? Objects are sometimes referred to as Instances. When you create an object, you are making an instance of the class.


3. Instantiation and Constructors

How do we turn the static blueprint (the class) into a functional, memory-resident entity (the object)? We use Instantiation.

3.1 Instantiation (The Creation Process)

Instantiation is the process of creating a new object from a class definition.

Step-by-Step Analogy: Making a Cake (Instantiation)

1. The Class: The recipe for the cake (the blueprint).
2. The Instantiation: The actual process of mixing ingredients and baking.
3. The Object: The resulting cake, ready to be eaten (used).

When you write code like myCar = NEW Car(), you are performing instantiation. This tells the computer to allocate memory and build a new object based on the Car class definition.

3.2 Constructors (Initial Setup)

When an object is created during instantiation, there is usually a special method called immediately to set up the object. This is the Constructor.

A Constructor is a special type of method that is automatically called when a new object is instantiated from a class.

Its main job is to initialise the object to a given state, meaning it sets the initial, default, or starting values for the object's properties (attributes).

  • Example: When you create a new Car object, the constructor might automatically set Speed to 0, EngineState to Off, and assign the license plate number.

Common Mistake to Avoid: Don't confuse the constructor with other methods. While both are functions inside the class, the constructor is only executed *once* when the object is first created, whereas other methods (like Drive()) can be called multiple times during the object's lifetime.

✏ Key Takeaway (The Definition Chain)

The Class is the template.
The Object is a concrete entity based on the template.
Instantiation is the process of creating the object.
The Constructor is the special method run during instantiation to set the object's starting data (Properties).


4. Core OOP Concepts Overview (Syllabus Definitions)

The syllabus requires you to be familiar with the following concepts, which describe relationships and structural techniques used in OOP systems. We will briefly define them here.

Encapsulation

Encapsulation is the bundling of data (properties) and the methods that operate on that data into a single unit (the class/object). Crucially, it means that the internal workings and data representation of the object are hidden from the outside world, controlling access only through defined methods. This prevents accidental corruption of data.

Inheritance

Inheritance is a relationship where one class (the subclass or derived class) is based on, or is a more specialised version of, an existing class (the base class or parent class). The subclass automatically receives all the properties and methods of the base class.

  • Analogy: A Truck class inherits from a generic Vehicle class. The truck automatically gets the EngineSize property from Vehicle, but adds its own specialised property, like CargoCapacity.
Overriding

Overriding occurs when a subclass redefines (changes) a method that it inherited from its base class. This allows the subclass object to behave in a slightly different, more specialised way than the base class when that method is called.

  • Example: Both Vehicle and Truck have a StartEngine() method. The Truck class might override this method to include additional checks, reflecting that starting a heavy truck is different from starting a small car.
Association

Association is a relationship between two objects in which one object can make use of another object. It is a weaker relationship than inheritance (where one *is a* type of the other). It simply means two classes are linked because one needs the services or data of the other.

  • Example: In a football simulation, a Team object is associated with several Player objects. The team object "has" or "uses" the player objects.