1. Encapsulation

Definition: Hiding internal state and requiring all interaction to be performed through an object's methods.

  • Bundles data (fields) and methods into a single unit (class).
  • Uses access modifiers(private, public, protected) to restrict direct access.
  • Example: A BankAccount class hides balance details and provides controlled access via methods.

✅ Goal: Protect data and provide controlled access.

Example:
						public class BankAccount {
 private double balance; // private = hidden from outside

    public BankAccount(double initialBalance) {
        this.balance = initialBalance;
    }

    public void deposit(double amount) {
        if (amount > 0) balance += amount;
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) balance -= amount;
    }

    public double getBalance() {
        return balance;
    }
}
					

➡️ You can’t access balance directly — you must use the methods like getBalance() , deposit() , or withdraw() .

2. Inheritance

Definition: One class can inherit fields and methods from another (parent → child).

  • Allows a new class to derive properties and behaviors from an existing class.
  • Promotes code reuse and hierarchical relationships (extends keyword).
  • Example: A Dog class inherits generic properties from an Animal class.

✅ Goal: Code reuse and logical hierarchy.

Example:
						public class Animal {
    public void makeSound() {
        System.out.println("Animal sound");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Bark");
    }
}

Dog dog = new Dog();
dog.makeSound(); // Output: Bark
					

3. Polymorphism

Definition: Objects can take many forms, usually via method overriding or interfaces.

  • Enables objects to take multiple forms — typically via method overriding and overloading.
  • Allows calling overridden methods of a subclass using a parent class reference.
  • Example: An Animal class has a makeSound() method, but Cat and Dog implement it differently.

✅ Goal: Flexibility — one interface, many implementations.

Example:
						public class Animal {
 public void makeSound() {
 System.out.println("Animal sound");
 }
}
public class Cat extends Animal {
 public void makeSound() {
 System.out.println("Meow");
 }
}
public class Dog extends Animal {
 public void makeSound() {
 System.out.println("Bark");
 }
}
Animal myAnimal = new Dog();
myAnimal.makeSound(); // Output: Bark
myAnimal = new Cat();
myAnimal.makeSound(); // Output: Meow
					

✅ The same method makeSound() behaves differently based on the object.

4. Abstraction

Definition: Hiding complex implementation details and showing only essential features.

  • Hides complex implementation and exposes only essential features.
  • Achieved using abstract classes and interfaces.
  • Example: A Shape class might define area() but leave specifics to subclasses (Circle, Rectangle).

✅ Goal: Simplify the interface and reduce complexity

Example using abstract class:
						public abstract class Shape {
 public abstract double area();
}
public class Circle extends Shape {
 private double radius;
 public Circle(double radius) {
 this.radius = radius;
 }
 @Override
 public double area() {
 return Math.PI * radius * radius;
 }
}
Shape shape = new Circle(5);
System.out.println(shape.area()); // Output: 78.5398...