Computer-science_A-level_Cie
-
computers-and-components6 主题
-
logic-gates-and-logic-circuits2 主题
-
central-processing-unit-cpu-architecture6 主题
-
assembly-language-4 主题
-
bit-manipulation1 主题
-
operating-systems3 主题
-
language-translators2 主题
-
data-security3 主题
-
data-integrity1 主题
-
ethics-and-ownership3 主题
-
database-concepts3 主题
-
database-management-systems-dbms-1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml1 主题
-
computational-thinking-skills1 主题
-
algorithms14 主题
-
data-types-and-records2 主题
-
arrays2 主题
-
files1 主题
-
introduction-to-abstract-data-types-adt1 主题
-
programming-basics1 主题
-
constructs2 主题
-
structured-programming1 主题
-
program-development-life-cycle2 主题
-
program-design-2 主题
-
program-testing-and-maintenance3 主题
-
user-defined-data-types1 主题
-
file-organisation-and-access-3 主题
-
floating-point-numbers-representation-and-manipulation3 主题
-
protocols2 主题
-
circuit-switching-packet-switching1 主题
-
processors-parallel-processing-and-virtual-machines5 主题
-
boolean-algebra-and-logic-circuits4 主题
-
purposes-of-an-operating-system-os3 主题
-
translation-software3 主题
-
encryption-encryption-protocols-and-digital-certificates3 主题
-
artificial-intelligence-ai4 主题
-
recursion1 主题
-
programming-paradigms4 主题
-
object-oriented-programming7 主题
-
file-processing-and-exception-handling2 主题
-
data-representation5 主题
-
multimedia3 主题
-
compression2 主题
-
networks-and-the-internet11 主题
encapsulation-oop
Encapsulation (OOP)
What is encapsulation?
-
In A Level Computer Science, encapsulation refers to the practice of grouping data (attributes) and methods (functions) 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

Encapsulation in classes
Encapsulation in methods

Encapsulation in methods
Examiner Tips and Tricks
-
When determining whether a method or attribute is public or private, if neither keyword appears, then assume it is public
Programming encapsulation (OOP)
What is a get method?
-
The “get” method is a common naming convention for a type of method that is used to retrieve the value of an object’s private attributes (instance variables)
-
These methods are also known as “getter” methods or “accessor” methods
-
The main purpose of a get method is to provide controlled access to the internal state (data) of an object without allowing direct modification
-
Attributes are often declared as private to achieve encapsulation, so cannot be accessed directly from outside the class
-
External code can use the public get methods to read the values of these private attributes (also known as instance variables)
Pseudocode

Pseudocode for a get method
Java
//creating a class called NumberUpdater public class NumberUpdater { //setting the private attribute to the value of 10 private int number = 10; //creating a get method that will return the value store in the number attribute public int getNumber() { return number; } }
Python
#creating a class called NumberUpdater class NumberUpdater: #setting the private attribute to the value of 10 def __init__(self): self.__number = 10 #creating a get method that will return the value stored in the number attribute @property def number(self): return self.__number
What is a set method?
-
The “set” method is a common naming convention for a type of method that is used to set the value of object’s private instance variables
-
Setter methods, also known as “setter” methods or “mutator” methods
-
Generally additional code is written within the method to allow controlled access by enforcing certain conditions or validations before updating the attribute
Pseudocode

Pseudocode for a “setter” method
Java
//creating a class called NumberUpdater public class NumberUpdater //setting attribute value to 10 private int oldnumber = 10; //creating a set method public void setNumber(int newnumber) { //if the new number is less than zero if (newnumber < 0) { //oldnumber is kept the same oldnumber = oldnumber; } else { //oldnumber is updated with the new number value oldnumber = newnumber; } } }
Python
//creating a class called NumberUpdater class NumberUpdater: //setting attribute value to 10 def __init__(self): self.__oldnumber = 10 //creating a set method def setNumber(self, newnumber): //if the new number is less than zero if newnumber < 0: //oldnumber is kept the same self.__oldnumber = self.__oldnumber else: //oldnumber is updated with the new number value self.__oldnumber = newnumber
Worked Example
A computer game is being designed that will include different vehicles. A prototype for the game is being developed using object‑oriented programming.
The class Vehicle stores data about the vehicles. Each vehicle has an identification name, a maximum speed, a current speed and a horizontal position. The value IncreaseAmount is added to the current speed each time the vehicle increases its speed.

Write program code for the get methods GetCurrentSpeed(),
GetIncreaseAmount(), GetMaxSpeed() and GetHorizontalPosition() [3]
Answer
-
1 get function header (and end where appropriate) with no parameter … [1 mark]
-
…returning attribute (without overwriting) [1 mark]
-
3 further correct get methods [1 mark]
Example program code:
VB.NET
Function GetCurrentSpeed() Return CurrentSpeed
End Function
Function GetIncreaseAmount() Return IncreaseAmount
End Function
Function GetHorizontalPosition() Return HorizontalPosition
End Function
Function GetMaxSpeed() Return MaxSpeed
End Function
Java
public Integer GetCurrentSpeed(){ return CurrentSpeed;
}
public Integer GetIncreaseAmount(){ return IncreaseAmount;
}
public Integer GetHorizontalPosition(){ return HorizontalPosition;
}
public Integer GetMaxSpeed(){ return MaxSpeed;
}
Python
def GetCurrentSpeed(self): return self.__CurrentSpeed
def GetIncreaseAmount(self): return self.__IncreaseAmount
def GetHorizontalPosition(self): return self.__HorizontalPosition
def GetMaxSpeed(self): return self.__MaxSpeed
Responses