Methods Concept
ℹ️
In object-oriented programming, methods define the actions that an object can perform. They are functions within a class that describe the behaviors or operations of the objects created from the class.
Methods are integral to how objects interact with each other and perform tasks. Let’s explore this concept using the BankAccount
class example:
ㅤㅤㅤㅤㅤBankAccountㅤㅤㅤㅤㅤ |
---|
deposit(amount) withdraw(amount) |
Bank Account Example:
For instance, let’s use the BankAccount
class to create two bank account objects:
ㅤㅤㅤㅤㅤaccount1ㅤㅤㅤㅤㅤ | ㅤㅤㅤㅤㅤaccount2ㅤㅤㅤㅤㅤ |
---|---|
accountNumber: 123456 balance: 500 accountHolder: John Doe | accountNumber: 789012 balance: 1000 accountHolder: Jane Smith |
deposit(amount) withdraw(amount) | deposit(amount) withdraw(amount) |
Method Usage:
account1
can use thedeposit
method to add funds and thewithdraw
method to withdraw funds.account2
has the same methods but can performdeposit
andwithdraw
operations based on its own balance and account details.
Each object has access to the same methods defined in the class, which enables them to perform similar actions, but the outcomes of these methods depend on the object’s specific attributes and current state.
Explore the Core Principles chapter to learn more about how methods integrate with the core principles of OOP, including inheritance, polymorphism, abstraction, and encapsulation.