📖 What is OOP?
A way to write programs using "objects" that hold data and actions. Code is organized around real-world things instead of just functions.
Why OOP? Before OOP, programs were written procedurally - code executed top-to-bottom sequentially. As programs grew larger, this became difficult to manage, debug, and maintain. OOP solves this by organizing code into logical units (objects) that mirror real-world entities.
💡 Real-life Example:
Think of a Car:
• Object: Your specific red Toyota car parked outside
• Attributes (Data): color="red", model="Toyota", speed=0, fuel=50L, engine="running"
• Methods (Behavior): start(), stop(), accelerate(), brake(), refuel()
📌 Six Key OOP Concepts (Detailed Explanation):
1. Class:
A blueprint/template that defines what data and actions an object will have. No memory is used until an object is actually created from it.
// Class definition (blueprint)
class Car {
// Attributes/Properties
String color;
String model;
int speed;
// Behavior/Methods
void start() {
System.out.println("Car started");
}
void accelerate() {
speed += 10;
System.out.println("Speed: " + speed);
}
}
2. Object:
An actual instance made from a class with real values in memory. Class = cookie cutter, Object = actual cookie. Many objects can be made from one class, each with different data.
// Creating objects (actual instances)
Car myCar = new Car();
myCar.color = "Red";
myCar.model = "Toyota";
myCar.speed = 0;
myCar.start(); // Output: Car started
myCar.accelerate(); // Output: Speed: 10
Car yourCar = new Car();
yourCar.color = "Blue";
yourCar.model = "Honda";
// Different object, different values!
3. Encapsulation:
Wrapping data + methods in one class and hiding data using access modifiers (private/public). Outside code accesses data only through public methods — like a capsule hiding medicine inside.
💡 Real-life Analogy:
ATM machine - You can check balance and withdraw money (public methods), but you cannot directly access the vault or change your balance manually (private data).
class BankAccount {
// Private data (hidden from outside)
private double balance;
private String accountNumber;
// Public methods (controlled access)
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance; // Read-only access
}
}
BankAccount acc = new BankAccount();
// acc.balance = 10000; // ERROR! Cannot access private
acc.deposit(5000); // OK! Using public method
4. Inheritance:
Child class gets all properties and methods of a parent class and can add its own. Promotes reusability — write once, use many times. (IS-A rule: Dog IS-A Animal)
💡 Real-life Analogy:
You inherit features from your parents (eye color, height genes) but you're still a unique person with additional qualities.
// Parent class
class Vehicle {
String brand;
int speed;
void start() {
System.out.println("Vehicle starting...");
}
}
// Child class inherits from Vehicle
class Car extends Vehicle {
int doors = 4;
void drive() {
System.out.println("Car driving...");
}
}
Car myCar = new Car();
myCar.brand = "Toyota"; // Inherited from Vehicle
myCar.start(); // Inherited method
myCar.drive(); // Own method
5. Polymorphism:
Same method name, different behavior depending on the object. Two types: overloading (same class, different params) and overriding (child redefines parent method).
💡 Real-life Analogy:
A person can be a student at college, son at home, employee at work - same person, different behaviors in different contexts.
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Woof! Woof!");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Meow! Meow!");
}
}
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound(); // Output: Woof! Woof!
a2.sound(); // Output: Meow! Meow!
// Same method name, different behavior!
6. Abstraction:
Hide complex details, show only what the user needs. Focus on "what it does" not "how." Like using a car's accelerator — you don't need to know how the engine works.
💡 Real-life Analogy:
TV Remote - You press "Volume Up" button (simple interface) but don't know the complex electronic circuits inside that actually increase volume.
// Abstract class (cannot create object)
abstract class Vehicle {
// Abstract method (no implementation)
abstract void start();
// Concrete method (has implementation)
void stop() {
System.out.println("Vehicle stopped");
}
}
class Car extends Vehicle {
// Must provide implementation
void start() {
System.out.println("Car engine started");
}
}
// Vehicle v = new Vehicle(); // ERROR!
Car c = new Car();
c.start(); // User doesn't see complex implementation