Classes (OOP) (OCR A-Level Computer Science): Revision Notes
Classes (OOP)
Overview
In Object-Oriented Programming (OOP), a class is a blueprint or template used to define and create objects. It provides a structured way to represent real-world entities by defining the shared properties and behaviours that instances (objects) of the class will have. Classes are central to OOP and enable code organisation, reuse, and modularity.
Key Characteristics of Classes
Defining a Class
- A class is defined with a specific name that represents a type or category of objects, such as Person, Car, or BankAccount.
- The class name is generally written in capitalised camel case (e.g., Animal, StudentRecord) to distinguish it from other parts of the code.
Example: In Python, for instance, the class keyword is used to define a new class:
class ExampleClass:
pass # This is an empty class for demonstration
Structure of a Class
- The structure of a class consists of a class name and a body, where attributes and methods can be defined (although in this overview, we're focusing only on the concept of the class itself).
- The class body is where the properties and potential actions of all instances of the class are outlined.
- Each instance of the class will automatically share this structure.
Purpose of a Class
- Blueprint: A class provides a standardised blueprint for creating multiple similar entities (objects) with a consistent structure. For example, a Car class might outline general characteristics of cars without focusing on any specific car.
- Code Organisation: Classes help keep related code organised in a single, reusable structure, making complex programmes more manageable.
- Reuse and Modularity: Defining a class allows a programmer to reuse its structure repeatedly, allowing efficient, modular code design and reducing redundancy.
Creating an Instance (Without Detailing)
- A class alone is a conceptual structure; it defines what an object of this type should have or be able to do, but it does not represent any particular instance of the entity.
- When a class is "instantiated," it creates an actual instance of that blueprint.
- However, instantiation itself does not alter the class—it only produces individual instances based on that class's design.
Example of a Class Definition
In Python, a class might look something like this, though this example is kept simple to focus on the concept of defining a class.
class Vehicle:
pass
Here, Vehicle is a class, serving as a general template for any kind of vehicle.
It does not specify any details but sets up a structure that can later be filled in with properties that vehicles share, like make, model, or speed.
Class as a Conceptual Model
- Classes allow programmers to conceptualise real-world entities in a way that makes them manageable in code.
- For instance, rather than having to redefine characteristics and behaviours for every individual vehicle in a programme, one can define the Vehicle class once and use it as a model for many instances of vehicles.
- This abstraction makes it easier to develop complex systems by breaking down real-world or conceptual entities into standardised, reusable components.
Note Summary
Key Takeaways
- A class is a blueprint in OOP, defining a template for creating instances (objects) with a consistent structure and behaviour.
- Classes improve code organisation by grouping related properties and behaviours in a unified structure.
- They promote reusability and modularity, central principles of efficient software design.