Java OOPs (Object-Oriented Programming) - Detailed Explanation
Object-Oriented Programming (OOP) is a programming paradigm
based on the concept of objects,
which contain data (attributes) and methods (behavior). Java follows
OOP principles to create
modular, reusable, and scalable applications.
1. Four Pillars of OOP in Java
The four fundamental concepts of OOP are:
Encapsulation - Binding data and methods together in a class.
Inheritance - Acquiring properties and behaviors of another class.
Polymorphism - Ability to take multiple forms (method overloading &
overriding).
Abstraction - Hiding implementation details and showing only the
essential features.
2. Real-World Example: Bank System (Low-Level Design)
Objects in a bank system:
- Customer (name, account details, balance)
- BankAccount (account number, balance, deposit, withdraw)
- LoanAccount (loan amount, interest rate)
- Transaction (date, amount, transaction type)
3. Implementing OOP Concepts in Java
Encapsulation (Data Hiding)
Encapsulation ensures that data is hidden from direct access and
modified through methods.
Example:
class Customer {
private String name;
private String accountNumber;
public Customer(String name, String accountNumber) {
this.name = name;
this.accountNumber = accountNumber;
}
// Getters and setters to access private variables
public String getName() { return name; }
public String getAccountNumber() { return accountNumber; }
}
Real-world analogy: In a bank, your personal details are private and
can only be accessed by
authorized persons (e.g., bank tellers).
Inheritance (Code Reusability)
Inheritance allows one class (child) to inherit properties and behavior
from another class.
Example:
class BankAccount {
protected double balance;
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance!");
}
}
}
Real-world analogy: A Savings Account is a specialized version of a
Bank Account.
Polymorphism (Method Overloading & Overriding)
Polymorphism allows methods to have different implementations
depending on the object.
Example:
class Transaction {
void processPayment(double amount) {
System.out.println("Processing payment of $" + amount);
}
void processPayment(double amount, String accountNumber) {
System.out.println("Processing payment of $" + amount + " to
account " + accountNumber);
}
}
Real-world analogy: A credit card payment can be processed
differently than a debit card payment.
Abstraction (Hiding Implementation Details)
Abstract classes and interfaces hide complex details and enforce a
contract for implementation.
Example:
abstract class Account {
protected double balance;
abstract void deposit(double amount);
}
class CheckingAccount extends Account {
@Override
void deposit(double amount) {
balance += amount;
}
}
Real-world analogy: You use an ATM to deposit money, but you dont
see the internal workings.
... (More sections included in the PDF) ...