OCR A Level Computer Science: 2.5 Object Oriented Languages
OCR A Level Computer Science: 2.5 Object Oriented Languages
Page 1 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Classes (OOP)
Your notes
Classes (OOP)
What is a Class?
Classes are used as blueprints or templates that can be used to create objects within Object Oriented
Programming or OOP
An object is created from a specific instance of a class and has its own state and behaviours
Using this method allows for reusable and organised code in a modular way
Consider a class of students
Each student has a name, a date of birth and gender
Therefore we can create a blueprint or template for all students by making a class which contains these
three attributes
As each student (or object) is created they will each have their own name, date of birth and gender
attributes along with their own state for example
Name “John”
Date of birth “06/10/2015”
Gender “Male”
Some classes are already prebuilt into a programming language saving the developer from having to
write them from scratch and often provide common functionality
Examples from Java include:
Date and calendar when working with dates
String when working with strings of text
Random when generating random numbers
Scanner when reading input from a user or file
Custom classes are created by the programmer to define new data types
For example, a class for animals does not exist and so the programmer must define a custom class
Page 2 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 3 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
EXAM TIP
Although you may see the terms methods/functions/ procedures, a method can be either a
function or a procedure
A function is a method that must return a value
A procedure is a method that does not need to return a value
WORKED EXAMPLE
Page 4 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 5 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Objects (OOP)
Your notes
Objects (OOP)
What is an Object?
An object is a representation of a real-world entity eg teacher, aeroplane, mobile phone, cat etc
A class is like a blueprint that describes the properties and behaviours of objects, while an object is a
specific instance created based on that blueprint with its own unique values for the properties
A constructor is a special method within a class that is automatically called when an object of that class
is created (instantiated)
Constructors typically define the initial values of instance variables and perform any necessary setup
to prepare the object for use
Page 6 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
WORKED EXAMPLE
A supermarket uses an object-oriented approach to organise items that it offers for sale. Part of
the class definition for the ItemForSale class is shown below:
class ItemForSale
public itemName
public price
public discount
Write a line of code to create an object of type ItemForSale called mushypeas that has a name of
“mushy peas” and a price of £0.89
Page 7 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
[3]
How to answer this question: Your notes
Creating object with identifier
mushypeas =
creating object as type
ItemForSale
mark for parameters passed in as needed ("mushy peas",0.89) [1 mark]
Answer:
Example answer 1 to get full marks:
mushypeas=new ItemForSale("mushy peas", 0.89)
Page 8 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Methods (OOP)
Your notes
Methods (OOP)
What is a Method?
Methods are fundamental in object-oriented programming (OOP) and are functions associated with
objects or classes that define the behaviour and actions that objects can perform
There are two main types of methods:
A function
A function performs a task and returns a value
A procedure
A procedure performs a task buts does nor return a value
For example, there are many different types of aircraft which all differ in design, but all require a ‘take
off’ and ‘landing’ method
An example of an aircraft class with both attributes and methods is shown below:
Page 9 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
EXAM TIP
In some OOP languages there are both instance methods and static methods
Instance methods
Instance methods are associated with individual instances (objects) of a class. They
operate on the specific data and properties of an object (see above on the Jumbo Jet
Page 10 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
object example)
Static methods
Your notes
Static methods are associated with a class itself and can be called without creating an
instance (object) of the class
Accessible and can be invoked by any code Private methods are only accessible within the same
within the same class or from any external class and cannot be invoked by external code or other
classes classes
Changes to public methods may have an Changes to private methods have a localized impact
impact on other parts of the codebase, so since they are only used internally within the class,
they should be carefully designed, providing flexibility to modify or refactor them without
documented, and backward compatible affecting other parts of the program
whenever possible
Public methods are used when you want to Private methods are used when you have internal
provide access to certain functionalities or implementation details that should not be accessed or
behaviours of an object or class to other parts used by external code. They are meant to be used only
of your program within the class itself for organizing and managing the
code internally
EXAM TIP
If a method does not specify the keyword public or private then its default value is set to public.
WORKED EXAMPLE
An object oriented system is implemented to organise further information about each worker’s
attendance. Classes, objects, methods and attributes are used in this system.
State the meaning of a method
[1]
Page 11 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Page 12 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Attributes (OOP)
Your notes
Attributes (OOP)
What is an Attribute?
In object-oriented programming (OOP), an attribute refers to a data member or a property associated
with an object or a class
They define the state of an object and can have different values for different instances of the same
class
Attributes can be of various data types, such as integers, strings, Booleans, or even other objects
Attributes can have different access rights
The example below shows a Car class object with an attribute called manufacturer
It has a private access meaning that it can be accessed only by instances of the Car class
The data that this attribute will hold must be of the String data type
The image below gives a visual representation of an object of this class being instantiated with a data
value of “Ford” :
Page 13 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
EXAM TIP
Attributes declared within methods (local variables) cannot have access modifiers because
they are local to the method and have a limited scope
Local variables are only accessible within the block or method in which they are declared. They
are not part of the class's state and cannot be accessed from other methods or classes
Page 14 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Inheritance (OOP)
Your notes
Inheritance (OOP)
What is Inheritance?
Inheritance is a key concept in object-oriented programming (OOP) that allows a class to inherit the
properties and behaviours (methods and attributes) of another class
Inheritance promotes code reuse by allowing derived classes to inherit and utilise the existing code
from the base class. This avoids duplicating code and promotes better organization and
maintainability
Inheritance establishes an "IS-A" relationship between the base class and the derived class
For example, if you have a base class called Vehicle and a derived class called Car, you can say that "a
Car is a Vehicle."
The car class inherits the properties and behaviours associated with being a vehicle
Inheritance involves two main entities:
The base class (also known as the parent class or superclass) and the derived class (also known as
the child class or subclass)
The derived class inherits the characteristics of the base class, meaning it can access and use the
methods and attributes defined in the base class
Page 15 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Page 16 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
WORKED EXAMPLE
The classes office and house inherit from building.
Describe what is meant by inheritance with reference to these classes.
[2]
How to answer this question:
1 mark per bullet up to a maximum of 2 marks, e.g:
When the child/derived/subclass class office/house takes on attributes/methods…
… from building / parent/base/superclass/ class
Answer:
Example answer to get full marks:
Page 17 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
When the derived classes "office" and "house" inherit attributes/methods [1] from the "building"
base class, they gain access to the properties and behaviours defined in the "building" class. [1]
Your notes
Page 18 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Encapsulation (OOP)
Your notes
Encapsulation (OOP)
What is Encapsulation?
Encapsulation in object-oriented programming is the practice of bundling data (attributes) and
methods (functions) together within a class
Using encapsulation ensures that data remains secure and is not accidentally modified or misused by
controlling access to them using access modifiers (e.g., public, private)
It also helps to organize code by keeping related data and methods together within an object
Encapsulation promotes code reusability, which means the same object or class can be used in
different parts of a program without rewriting the code
Encapsulation uses a concept called “Abstraction” which reduces complexity by hiding the
implementation details of the object, making it easier to understand and work with
Programmers can use methods and classes from other parts of the program without having to
understand how that it has been constructed internally
Encapsulation in Classes
Private variables are only accessible within the class itself, and external code cannot access them
directly.
Encapsulation hides how things work inside a class from the outside. External code can interact with
the class using public methods without needing to understand its internal details
Page 19 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Encapsulation in classes
Encapsulation in methods
EXAM TIP
When determining whether a method or attribute is public or private, if neither keyword
appears, then assume it is public
WORKED EXAMPLE
A taxi firm is investigating replacing its drivers with self-driving cars.
The code for the self-driving system has been written using an object-oriented programming
language.
It recognises obstacles in the road and then classifies them.
The class for Obstacle is shown below.
public class Obstacle
Page 20 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
moving=givenMoving
endprocedure
distance=givenDistance
endprocedure
endclass
Page 21 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Polymorphism (OOP)
Your notes
Polymorphism (OOP)
What is Polymorphism?
Polymorphism is a concept in programming that allows objects to take on different forms or
behaviours.
Different objects can share the same name or behaviour but can work in different ways
It helps make code more flexible, reusable, and easier to maintain
It allows flexibility and reusability in programming, making it easier to write and manage code
Objects can be treated as belonging to a common group, even if they belong to different classes,
making your code more versatile and adaptable to changes
Page 22 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
Page 23 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
Your notes
If either of the object types call the displayInfo() method, the program will execute the method from the
objects class as it overrides the Vehicle class method
Page 24 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources
For example
If a motorcycle object calls the displayInfo() method, "I am a Motorcycle!" will be output Your notes
If a Car object calls the displayInfo() method, "I am a Car!" will be output
Treating objects as common groups
Polymorphism also allows objects of different classes to be treated as objects of a common
superclass or base class
For example:
Vehicle vehicle1 = new Car()
This allows an array of type Vehicle to store both Motorcycle and Car objects rather than in separate
data structures
If the vehicle1.displayInfo() method is called, it will still output "I am a Car!"
If the vehicle2.displayInfo() method is called, it will still output "I am a Motorcycle!"
This flexibility provided by polymorphism are essential for creating more maintainable and modular
code
Page 25 of 25
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers