Attributes (OOP) (OCR A-Level Computer Science): Flashcards
📚Flashcards
Practise the cards
14 cards from this deck
ShowHide
Practise the cards
14 cards from this deck
Attributes in OOP
Attributes in OOP
Variables storing data specific to each class instance
What do attributes represent?
What do attributes represent?
The state of an object
Public attributes definition
Public attributes definition
Attributes accessible & modifiable directly from outside class
How to access public attributes
How to access public attributes
Using dot notation on an object
Drawback of public attributes
Drawback of public attributes
Can lead to unintended modifications
Private attributes definition
Private attributes definition
Attributes only accessed/modified within the class itself
Private attribute indicator in Python
Private attribute indicator in Python
Single (_) or double (__) underscore before attribute name
What principle do private attributes support?
What principle do private attributes support?
Encapsulation principle
Encapsulation definition
Encapsulation definition
Restricting direct access to an object's data
Purpose of getter methods (accessors)
Purpose of getter methods (accessors)
Allow controlled access to private attribute values
Purpose of setter methods (mutators)
Purpose of setter methods (mutators)
Allow modification of private attributes with validation
How setters enforce data integrity
How setters enforce data integrity
Validate data before modifying attributes
Benefit: private attrs & data security
Benefit: private attrs & data security
Ensures data not modified unintentionally or inappropriately
Error: accessing private attrs directly
Error: accessing private attrs directly
Attempting to access outside class causes AttributeError
