Programming Encapsulation (OCR A Level Computer Science): Revision Note

Exam code: H446

Becci Peters

Written by: Becci Peters

Reviewed by: James Woodhouse

Updated on

Programming Encapsulation

  • To program encapsulation, you should already have a solid understanding of what encapsulation in Object Oriented Programming (OOP) is.

How do you Define Encapsulation?

Get Methods

  • 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

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

Set Methods

  • 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

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

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Build on this topic

Becci Peters

Author: Becci Peters

Expertise: Computer Science Content Creator

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Portfolio Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.