Object Oriented Programming
Object Oriented Programming
programming
1. OOP presentation
2. Programming presentation
Java
OOP Concepts
• Classes and Objects
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction
1. Classes
and Objects
Class: A blueprint for creating objects. It defines a
datatype by bundling data and methods that work
on the data. Object: An instance of a class. It has
state and behaviour as defined by its class.
Example: The Bank Account class is a blueprint,
and account 1 and account 2 are objects
(instances) created from this blueprint.
2.
Encapsulati
on
Encapsulation is the concept of wrapping data
(attributes) and methods (functions) into a single
unit, the class. It restricts direct access to some of
the object's components, which can prevent the
accidental modification of data . Example: In the
Bank Account class, balance is encapsulated, and
it can only be modified via methods like deposit(),
withdraw(), and transfer().
Inheritance
Inheritance allows a class to inherit properties
and methods from another class. This helps to
reuse code and establish a relationship
between classes. Example: If you had a
Savings Account class that inherits from Bank
Account, it would automatically have all
methods and properties of Bank Account.
.
Polymorphi
sm
Polymorphism allows objects to be treated as
instances of their parent class, even if they have
different behaviours. It also allows methods to do
different things based on the object it is acting upon .
Example: If Savings Account and Checking Account
both inherit from Bank Account and have their own
withdraw method, they can be used interchangeably,
but each might behave differently in specific
situations.
Polymorphi
sm
Abstraction involves hiding the complex implementation
details and showing only the essential features of the object .
Example: Users of the Bank Account class do not need to know
how transfer() works internally; they just need to know that it
can be used to transfer money between accounts.
PROGRAMMIN
G- JAVA
Modify the Bank Account class to include a method
transfer() that allows transferring money
between two accounts
Class definition “Bank
Account”
deposit(amount): This method allows the user to deposit a certain amount into the bank
account .if (amount > 0): Checks if the deposit amount is positive. this. balance +=
amount;: If the amount is positive, it adds the amount to the current balance of the
account.console.log(...): Logs a message to the console showing the deposited amount
and the new balance .Else: If the deposit amount is not positive, it logs an error message.
Method ‘withdraw’
withdraw(amount): This method allows the user to withdraw a certain amount from the
bank account . if (amount > 0 && amount <= this.balance): Checks if the withdrawal
amount is positive and if the account has sufficient funds .this balance -= amount;: If both
conditions are met, it subtracts the amount from the current balance.console.log(...): Logs
a message to the console showing the withdrawn amount and the new balance.
Method ‘transfer’
transfer(amount, target Account): This method allows transferring money from one bank
account to another amount > 0 && amount <= this.balance: Checks if the transfer
amount is positive and if there are sufficient funds in the account this balance -= amount;:
Subtracts the amount from the current account (this).target Account balance += amount;:
Adds the amount to the target Account's balance.console.log(...): Logs messages showing
the transfer details, including the new balances of both accounts.
Testing the ‘transfer’ Method