3. Java OOP Fundamentals, Four Pillars & Interfaces

Chapters 10-13: Classes, Objects, Constructors, Encapsulation, Inheritance, Overriding, Polymorphism, Interfaces & Access Modifiers

Module 1: OOP Fundamentals & Four Pillars of Object-Oriented Design

Structuring applications around objects encapsulating state and behavior.

🇬🇧 English Definition

The four pillars of OOP are: Encapsulation (hiding state), Inheritance (reusing parent behavior), Polymorphism (one interface, multiple implementations), and Abstraction (exposing essential features).

🇮🇳 Hinglish Definition

OOP ke 4 pillars hain: Encapsulation, Inheritance, Polymorphism aur Abstraction. Class blueprint hai aur Object live instance hai.

Core Concepts & Rules

  • Encapsulation: Restricts direct access to fields using private modifier and public getters/setters.
  • Polymorphism: Parent reference pointing to child object (Animal a = new Dog(); a.sound();).
  • Method Overriding: Subclass redefines parent method using @Override annotation.

🔍 Line-by-Line Code Breakdown

  1. Line 1-5: Parent class Animal with method sound().
  2. Line 6-10: Child class Dog overriding sound() with @Override.
  3. Line 13: Polymorphic reference Animal a = new Dog() calling Dog's sound().

🎯 Interview Focus & Questions

  • Q: Four Pillars of OOP?
    A: Encapsulation, Inheritance, Polymorphism, and Abstraction.
  • Q: Difference between Overloading and Overriding?
    A: Overloading changes parameters at compile-time; Overriding redefines method implementation at runtime.
💻 Practical Java Code Example - OOP Fundamentals & Four Pillars of Object-Oriented Design
class Animal {
    void sound() { System.out.println("Animal sound"); }
}

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

public class OOPDemo {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
📤 Compiled Execution Output:
Bark

Module 2: Interfaces, Abstract Classes & Access Modifiers

Defining behavioral contracts and controlling member visibility across packages.

🇬🇧 English Definition

An interface defines a contract that classes implement. Abstract classes provide partial implementation and shared state. Access modifiers (public, protected, package-private, private) control visibility.

🇮🇳 Hinglish Definition

Interface contract hai, class multiple interfaces implement kar sakti hai. Abstract class partial abstraction aur common state ke liye useful hai.

Core Concepts & Rules

  • Multiple Interfaces: Java classes can implement multiple interfaces (class C implements A, B).
  • Interface vs Abstract Class: Interfaces focus on contracts without instance fields; Abstract classes can carry instance state and constructors.

🔍 Line-by-Line Code Breakdown

  1. Line 1-3: Interface Payment declaring abstract method pay(double amount).
  2. Line 4-8: Class UPI implementing Payment interface.

🎯 Interview Focus & Questions

  • Q: Interface vs Abstract Class?
    A: Classes can implement multiple interfaces but extend only one abstract class. Interfaces define contracts.
💻 Practical Java Code Example - Interfaces, Abstract Classes & Access Modifiers
interface Payment {
    void pay(double amount);
}

class UPI implements Payment {
    public void pay(double amount) {
        System.out.println("Paid ₹" + amount + " via UPI");
    }
}

public class InterfaceDemo {
    public static void main(String[] args) {
        Payment p = new UPI();
        p.pay(500.0);
    }
}
📤 Compiled Execution Output:
Paid ₹500.0 via UPI