Attributes (OOP) (OCR A-Level Computer Science): Flashcards

📚Flashcards
Attributes (OOP)
Sign up to keep revising.Create a free account to study more flashcards and track your progress.

Practise the cards

14 cards from this deck

Show

Attributes in OOP

Variables storing data specific to each class instance

What do attributes represent?

The state of an object

Public attributes definition

Attributes accessible & modifiable directly from outside class

How to access public attributes

Using dot notation on an object

Drawback of public attributes

Can lead to unintended modifications

Private attributes definition

Attributes only accessed/modified within the class itself

Private attribute indicator in Python

Single (_) or double (__) underscore before attribute name

What principle do private attributes support?

Encapsulation principle

Encapsulation definition

Restricting direct access to an object's data

Purpose of getter methods (accessors)

Allow controlled access to private attribute values

Purpose of setter methods (mutators)

Allow modification of private attributes with validation

How setters enforce data integrity

Validate data before modifying attributes

Benefit: private attrs & data security

Ensures data not modified unintentionally or inappropriately

Error: accessing private attrs directly

Attempting to access outside class causes AttributeError

Join 100,000+ A-Level students studying Flashcards with us.

Select your subjects, and get access to A+ resources today.