Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Attributes (OOP) quickly and effectively.
369+ students studying
In Object-Oriented Programming (OOP), attributes are variables that store data specific to each instance of a class. They represent the state of an object, holding values that define its unique characteristics. Attributes can be either public or private, which affects how they can be accessed and modified. Understanding attributes is essential for implementing data security, modularity, and flexibility in OOP.
Example:
class Car:
def __init__(self, color):
self.color = color # Public attribute
car1 = Car("red")
print(car1.color) # Outputs: red
car1.color = "blue" # Directly modify the color
_
) or a double underscore (__
) before the attribute name.Example:
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute
account = BankAccount(100)
# print(account.__balance) # Error: Attribute is not directly accessible
Since private attributes cannot be accessed directly from outside the class, getter and setter methods (also known as accessors and mutators) are used to control access and modification.
Purpose: Getter methods allow external code to access the value of a private attribute without directly exposing it. This enables controlled and secure data retrieval.
Example:
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def get_balance(self):
return self.__balance # Accessor method to retrieve balance
account = BankAccount(100)
print(account.get_balance()) # Outputs: 100
Purpose: Setter methods allow external code to modify the value of a private attribute, with any necessary validation or restrictions applied within the method. This helps enforce data integrity.
Example:
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def set_balance(self, amount):
if amount >= 0: # Only allow non-negative balance
self.__balance = amount
else:
print("Invalid amount: Balance cannot be negative.")
account = BankAccount(100)
account.set_balance(200) # Sets balance to 200
print(account.get_balance()) # Outputs: 200
account.set_balance(-50) # Error: Invalid amount
Below is a complete example illustrating the use of private attributes and getter/setter methods to control access to an account balance.
class BankAccount:
def __init__(self, account_holder, balance=0):
self.account_holder = account_holder # Public attribute
self.__balance = balance # Private attribute
# Getter for balance
def get_balance(self):
return self.__balance
# Setter for balance with validation
def set_balance(self, amount):
if amount >= 0:
self.__balance = amount
else:
print("Error: Balance cannot be negative.")
# Additional method to deposit funds
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"${amount} deposited. New balance: ${self.__balance}")
else:
print("Error: Deposit amount must be positive.")
Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
70 flashcards
Flashcards on Attributes (OOP)
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards7 quizzes
Quizzes on Attributes (OOP)
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Attributes (OOP)
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Attributes (OOP)
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Attributes (OOP)
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Attributes (OOP) to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered