Attributes (OOP) (Cambridge (CIE) A Level Computer Science): Revision Note

Exam code: 9618

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

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” :

An example instance of an object

An example instance of an object

  • In most cases each class has many different attributes

  • Below is an example of an object of class "person":

Example of an object of class "person"

Example of an object of class "person"

Examiner Tips and Tricks

  • 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

Programming attributes (OOP)

How do you program attributes?

Pseudocode

Example of a created class, "Person", containing several attributes

Example of a created class, "Person", containing several attributes

Java 

public class Person {

    // Attributes for the person class

    private String name;

    private int age;

    private String gender;

    private String occupation;

    private boolean isMarried;

}

Python

  • In Python attributes are defined using the self keyword followed by the attribute name and its initial value

class MyClass:

   def __init__(self, attribute1, attribute2):

      # Define attributes

      self.attribute1 = attribute1

      self.attribute2 = attribute2

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

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

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject 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.