You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers the fundamental building blocks of Object-Oriented Programming (OOP) for the OCR A-Level Computer Science (H446) specification. You will learn about classes, objects, attributes, methods, constructors, and the this/self keyword.
A class is a blueprint or template that defines the structure and behaviour of a type of object. It specifies what attributes (data) and methods (behaviour) objects of that type will have.
An object is a specific instance of a class, created at runtime. Each object has its own set of attribute values.
| Term | Definition |
|---|---|
| Class | A template that defines attributes and methods for a type of object. |
| Object | A specific instance of a class, with its own attribute values. |
| Instantiation | The process of creating an object from a class. |
| Attribute | A variable belonging to an object that stores data about it. |
| Method | A function defined inside a class that operates on the object's data. |
CLASS Student
PRIVATE name: STRING
PRIVATE age: INTEGER
PRIVATE grades: ARRAY OF INTEGER
PUBLIC PROCEDURE new(n: STRING, a: INTEGER)
name = n
age = a
grades = []
END PROCEDURE
PUBLIC FUNCTION getName() RETURNS STRING
RETURN name
END FUNCTION
PUBLIC PROCEDURE setName(n: STRING)
name = n
END PROCEDURE
PUBLIC PROCEDURE addGrade(grade: INTEGER)
grades.append(grade)
END PROCEDURE
PUBLIC FUNCTION getAverageGrade() RETURNS REAL
IF LENGTH(grades) = 0 THEN
RETURN 0
END IF
total = 0
FOR i = 0 TO LENGTH(grades) - 1
total = total + grades[i]
NEXT i
RETURN total / LENGTH(grades)
END FUNCTION
END CLASS
class Student:
def __init__(self, name: str, age: int):
self.__name = name # Private attribute
self.__age = age # Private attribute
self.__grades = [] # Private attribute
def get_name(self) -> str:
return self.__name
def set_name(self, name: str):
self.__name = name
def add_grade(self, grade: int):
self.__grades.append(grade)
def get_average_grade(self) -> float:
if len(self.__grades) == 0:
return 0
return sum(self.__grades) / len(self.__grades)
Instantiation is the process of creating an object from a class. When you instantiate a class, the constructor method runs to initialise the object's attributes.
# Creating (instantiating) objects
student1 = Student("Alice", 17)
student2 = Student("Bob", 18)
# Each object has its own attribute values
print(student1.get_name()) # Alice
print(student2.get_name()) # Bob
# Objects are independent -- changing one does not affect the other
student1.add_grade(85)
student2.add_grade(72)
print(student1.get_average_grade()) # 85.0
print(student2.get_average_grade()) # 72.0
Exam Tip: The examiner may ask you to "create an instance" or "instantiate" a class. This simply means using the constructor to create a new object. You must show the class name followed by the constructor arguments.
Attributes are variables that belong to an object. They store the data that represents the object's state.
| Type | Description | Example |
|---|---|---|
| Instance attribute | Belongs to a specific object; each object has its own copy. | self.__name |
| Class attribute | Shared across ALL instances of the class; belongs to the class itself. | Student.count |
class Student:
count = 0 # Class attribute -- shared by all instances
def __init__(self, name: str):
self.__name = name # Instance attribute -- unique to each object
Student.count += 1 # Increment the shared class attribute
s1 = Student("Alice")
s2 = Student("Bob")
print(Student.count) # 2 (shared across all instances)
Methods are functions defined inside a class. They define the behaviour of objects and typically operate on the object's attributes.
| Type | Description |
|---|---|
| Instance method | Operates on a specific object's data; takes self as the first parameter. |
| Constructor | Special method called when an object is created (__init__ in Python). |
| Getter | Returns the value of a private attribute. |
| Setter | Sets the value of a private attribute, often with validation. |
| Static method | Belongs to the class rather than any instance; does not access instance data. |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.