1. What is Object-Oriented Programming (OOP)?
OOP is a programming model based on the concept of “objects,” which contain data (attributes) and methods (behavior). Java follows OOP principles, making it a popular language for building scalable and maintainable applications.
Key Benefits of OOP:
- Modularity: Code is organized into objects, making it easy to manage and modify.
- Reusability: Objects and classes can be reused across different applications.
- Scalability: Applications can be extended and modified with minimal changes.
- Security: Data hiding and encapsulation improve security.
2. Core OOPs Concepts in Java
Objects and Classes:
Definition: An object is an instance of a class, which is a blueprint for creating objects. A class defines the properties and behaviors of objects.
Real-World Example: A Car is an object of the Car class, which has properties like color, speed, and brand and behaviors like start(), stop(), and accelerate().
class Car {
String color;
int speed;
void start() {
System.out.println("Car is starting...");
}
void accelerate() {
System.out.println("Car is accelerating...");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.color = "Red";
myCar.speed = 100;
myCar.start();
myCar.accelerate();
}
}
Encapsulation:
Definition: Encapsulation is the process of wrapping data (variables) and methods into a single unit, restricting direct access to some components.
Real-World Example: Think of a bank account. The account balance is private and can only be accessed or modified through specific methods like deposit() and withdraw().
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
Inheritance:
Definition: Inheritance allows a child class to inherit the properties and methods of a parent class, promoting code reusability.
Real-World Example: A car is a specialized version of a vehicle. A Car class can inherit properties like speed and fuelCapacity from a Vehicle class.
class Vehicle {
int speed;
int fuelCapacity;
}
class Car extends Vehicle {
int numberOfDoors;
}
Polymorphism:
Definition: Polymorphism allows methods to take different forms, making the code more flexible and reusable.
Real-World Example: Consider a printer that can print different document types (PDF, Word, or Excel). The print() method can behave differently based on the input type.
lass Printer {
void print(String document) {
System.out.println("Printing document: " + document);
}
void print(int copies, String document) {
System.out.println("Printing " + copies + " copies of: " + document);
}
}
Abstraction:
Definition: Abstraction hides the implementation details and exposes only the necessary functionalities to the user.
Real-World Example: When using an ATM, you interact with options like withdraw() or checkBalance(), but you don’t see the backend operations processing the transactions.
abstract class ATM {
abstract void withdraw(double amount);
abstract void checkBalance();
}
class HDFC_ATM extends ATM {
private double balance = 5000;
@Override
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient Balance");
}
}
@Override
void checkBalance() {
System.out.println("Balance: " + balance);
}
}
