Objects (OOP) (OCR A-Level Computer Science): Revision Notes
Objects (OOP)
Overview
In Object-Oriented Programming (OOP), a class is a blueprint that defines the structure and behaviour of objects. An object is an instance of a class, representing a specific entity with attributes (data) and methods (functions) that define its state and behaviour. Understanding how to create and use objects is fundamental to OOP and is essential for writing organised, reusable, and modular code.
Objects in OOP
Defining and Creating Objects
- An object is a specific instance of a class, with its own values for the attributes defined in the class.
- To create an object, you call the class as though it were a function, passing in any necessary data for initialisation.
- This creates a unique instance with its own data, distinct from other instances of the same class.
Example:
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
# Creating objects of the Car class
car1 = Car("Toyota", "Corolla")
car2 = Car("Honda", "Civic")
Here, car1 and car2 are objects (or instances) of the Car class. Each object has its own unique values for make and model.
Object Attributes
- Attributes represent the state of an object and are defined within the class.
- Each object has its own set of attribute values, which can vary from one instance to another.
Example:
print(car1.make) # Output: Toyota
print(car2.make) # Output: Honda
Even though car1 and car2 are instances of the same class (Car), they have different values for the make attribute.
Object Methods
Methods are functions defined within a class that describe the behaviours of objects. They can use and modify the object's attributes.
Example:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, I'm {self.name} and I'm {self.age} years old.")
# Creating a Person object and calling its method
person1 = Person("Alice", 30)
person1.introduce() # Output: Hi, I'm Alice and I'm 30 years old.
Modifying Object Attributes
You can change the values of an object's attributes directly or by using methods, allowing objects to update their state during the programme's execution.
Example:
person1.age = 31 # Directly modifying an attribute
print(person1.age) # Output: 31
Interacting with Multiple Objects
Each object operates independently, so you can create multiple instances of a class, each with its own attribute values and behaviours.
Example:
person2 = Person("Bob", 25)
person1.introduce() # Output: Hi, I'm Alice and I'm 31 years old.
person2.introduce() # Output: Hi, I'm Bob and I'm 25 years old.
Example Programme: Objects in Action
Below is an example programme that defines a Book class and creates multiple Book objects.
class Book:
def __init__(self, title, author, year):
self.title = title
self.author = author
self.year = year
def get_description(self):
return f"{self.title} by {self.author}, published in {self.year}"
# Creating Book objects
book1 = Book("1984", "George Orwell", 1949)
book2 = Book("To Kill a Mockingbird", "Harper Lee", 1960)
# Accessing attributes and calling methods on objects
print(book1.get_description()) # Output: 1984 by George Orwell, published in 1949
print(book2.get_description()) # Output: To Kill a Mockingbird by Harper Lee, published in 1960
In this example:
- book1 and book2 are objects of the Book class, each with its values for title, author, and year.
- The get_description method displays the details of each book, showing how each object retains its unique data.
Tracing and Modifying Objects
Tracing Object Attributes:
To trace an object, you can inspect its attribute values at any point in the programme.
Example:
print(book1.title) # Outputs: 1984
Modifying an Object's State:
You can update an object's state by changing its attributes directly or through methods.
Example:
book1.year = 1950 # Updates the publication year
print(book1.get_description()) # Output: 1984 by George Orwell, published in 1950
Note Summary
Key Takeaways
- Object Creation: Objects are created from classes and hold their state through attributes.
- Independent Instances: Each object operates independently, with its data and methods.
- Encapsulation of Data and Behaviour: Objects bundle both data (attributes) and actions (methods), keeping them together and providing modular functionality.