Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Methods (OOP) quickly and effectively.
463+ students studying
In Object-Oriented Programming (OOP), methods are functions defined within a class that represent the behaviours of an object. They can manipulate an object's attributes and perform actions associated with the class. Like attributes, methods can be public or private, determining their accessibility within and outside of the class. Understanding methods, and the distinction between public and private methods, is essential for effective OOP.
Example:
class Calculator:
def add(self, a, b):
return a + b # Public method accessible from outside the class
calc = Calculator()
print(calc.add(3, 5)) # Outputs: 8
Example:
class BankAccount:
def __init__(self, balance=0):
self.balance = balance
def deposit(self, amount):
if amount > 0:
self.balance += amount
self._log_transaction(amount) # Calls private method
def _log_transaction(self, amount): # Private method
print(f"Transaction: Deposited ${amount}. New balance: ${self.balance}")
account = BankAccount()
account.deposit(100) # Outputs: Transaction: Deposited $100. New balance: $100
# account._log_transaction(50) # Error: Cannot directly access private method
Below is an example illustrating the use of public and private methods in a BankAccount class.
class BankAccount:
def __init__(self, account_holder, balance=0):
self.account_holder = account_holder
self.balance = balance
# Public method for depositing money
def deposit(self, amount):
if amount > 0:
self.balance += amount
self._log_transaction("deposit", amount)
# Public method for withdrawing money
def withdraw(self, amount):
if amount > 0 and amount <= self.balance:
self.balance -= amount
self._log_transaction("withdrawal", amount)
else:
print("Insufficient funds or invalid amount.")
# Public method to check balance
def check_balance(self):
return self.balance
# Private method to log transactions
def _log_transaction(self, transaction_type, amount):
print(f"{transaction_type.capitalise()} of ${amount}. New balance: ${self.balance}")
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 Methods (OOP)
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards7 quizzes
Quizzes on Methods (OOP)
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Methods (OOP)
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Methods (OOP)
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Methods (OOP)
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Methods (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