Skip to main content

Command Palette

Search for a command to run...

Java OPPS

Updated
β€’8 min readβ€’View as Markdown

πŸ”Ή Core OOP Concepts in Java

Great question πŸ‘ Let’s go step by step β€” OOPs (Object-Oriented Programming System) is not just a coding style, it’s a paradigm shift in programming.


πŸ”Ή 1. What is OOPs?

πŸ‘‰ Object-Oriented Programming System (OOPs) is a programming paradigm based on the concept of objects (real-world entities) that contain data (fields) and behavior (methods).

  • In OOP, everything revolves around classes and objects.

  • It focuses on reusability, modularity, security, and scalability.


πŸ”Ή 2. Why OOPs Came?

Before OOP, programming was mainly Procedural Programming (C, Pascal, etc.).
Procedural programming had problems:

  • Code was not reusable (duplicate logic everywhere).

  • Data was exposed (no security, anyone could access it).

  • Complexity increased for large projects.

  • Hard to maintain and modify.

πŸ‘‰ OOP was introduced to solve these by modeling software closer to real-world objects.


πŸ”Ή 3. When OOPs Came?

  • Concept of objects first appeared in 1960s (Simula 67, the first object-oriented language).

  • Popularized in 1980s–1990s with languages like Smalltalk, C++ (1983), Java (1995).

  • Today, most modern languages (Java, Python, C#, Kotlin, etc.) are OOP-based or support OOP.


πŸ”Ή 4. What Problems Does OOP Solve?

Problem in Procedural ProgrammingSolution by OOP
No data security β†’ anyone can modify variablesEncapsulation hides data (private fields + getters/setters)
Large codebase hard to maintainModularity β†’ divide into classes
Code duplicationInheritance allows reusability
No flexibility (one function fits all)Polymorphism allows different behaviors with same interface
Hard to model real-world problemsObjects represent real-world entities (Car, BankAccount, Employee)
Poor scalabilityOOP projects can grow easily (extensible)

πŸ”Ή 5. Core OOP Principles (Pillars)

  1. Encapsulation – data hiding & security

  2. Abstraction – hide implementation, show only essential

  3. Inheritance – reusability, IS-A relationship

  4. Polymorphism – one interface, many implementations


βœ… In short:
OOP (introduced in 1960s, popular in 1980s–90s) came to solve the problems of procedural programming like lack of reusability, poor maintainability, and data insecurity. It models software closer to real-world objects and makes code modular, reusable, secure, and scalable.

Here’s the timeline of OOP evolution πŸ“Š:

  • 1960 β†’ Procedural Programming (C, Pascal)

  • 1967 β†’ Simula 67 (First OOP Language)

  • 1980 β†’ Smalltalk (Pure OOP)

  • 1983 β†’ C++ (Hybrid: OOP + Procedural)

  • 1995 β†’ Java (Popularized OOP, Platform Independent)

  • 2025 β†’ Modern OOP (Java, Python, C#, Kotlin)

πŸ‘‰ This way you can remember when and why OOP came.


There are 4 main OOP pillars + some supporting principles.


1️⃣ Encapsulation

πŸ‘‰ "Wrapping data (fields) and methods (behaviors) together in a single unit (class)."

  • Achieved using private fields + public getters/setters.

  • Provides data hiding and security.

Example:

class BankAccount {
    private double balance; // data hidden

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

    public double getBalance() { // controlled access
        return balance;
    }
}

πŸ“Œ Here β†’ balance is hidden, only accessed through methods.


2️⃣ Abstraction

πŸ‘‰ "Hiding implementation details and showing only the essential features."

  • Achieved using abstract classes and interfaces.

  • Focuses on what to do, not how to do.

Example:

abstract class Shape {
    abstract void draw(); // abstract method
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square extends Shape {
    void draw() {
        System.out.println("Drawing Square");
    }
}

πŸ“Œ User only knows draw() works, not how it works.


3️⃣ Inheritance (IS-A relationship)

πŸ‘‰ "Acquiring properties and behaviors of a parent class into a child class."

  • Achieved using extends keyword.

  • Promotes reusability.

Example:

class Animal {
    void eat() {
        System.out.println("Animal eats");
    }
}

class Dog extends Animal { // IS-A relationship
    void bark() {
        System.out.println("Dog barks");
    }
}

πŸ“Œ Dog IS-A Animal.


4️⃣ Polymorphism (many forms)

πŸ‘‰ "Ability of an object to behave in multiple forms."

a) Compile-time Polymorphism (Method Overloading)

class Calculator {
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }
}

b) Runtime Polymorphism (Method Overriding)

class Animal {
    void sound() { System.out.println("Animal makes sound"); }
}
class Dog extends Animal {
    void sound() { System.out.println("Dog barks"); }
}

πŸ“Œ Overloading β†’ same method name, different parameters.
πŸ“Œ Overriding β†’ same method in parent and child, resolved at runtime.


πŸ”Ή Supporting OOP Concepts

5️⃣ Class & Object

  • Class β†’ Blueprint (e.g., Car).

  • Object β†’ Instance of a class (e.g., myCar).

class Car {
    void drive() { System.out.println("Car is driving"); }
}
Car c = new Car(); // object created

6️⃣ Constructor

  • Special method to initialize an object.

  • Same name as class, no return type.

class Student {
    String name;
    Student(String n) {
        name = n;
    }
}

7️⃣ Association, Aggregation, Composition

  • Association β†’ General relationship between classes.

  • Aggregation (HAS-A) β†’ Weak relationship. (Car HAS-A Engine).

  • Composition (HAS-A strong) β†’ Strong ownership (House HAS-A Room).


8️⃣ Static vs Instance

  • Static β†’ Belongs to class, shared across objects.

  • Instance β†’ Each object gets its own copy.


9️⃣ Method Overloading vs Overriding

  • Overloading β†’ Compile-time polymorphism.

  • Overriding β†’ Runtime polymorphism.


πŸ”Ÿ Abstraction vs Encapsulation

FeatureAbstractionEncapsulation
WhatHides implementationHides data
Achieved byAbstract class, InterfacePrivate variables + getters/setters
Focus"What to do""How to protect data"

βœ… Final Summary

  • Encapsulation β†’ Hides data

  • Abstraction β†’ Hides implementation

  • Inheritance β†’ Reusability (IS-A)

  • Polymorphism β†’ Many forms (overloading/overriding)

  • HAS-A β†’ Composition/Aggregation

Perfect πŸ‘ Let me give you a single Java program that demonstrates all 4 OOPs pillars (Encapsulation, Abstraction, Inheritance, Polymorphism) in one shot. This is a very common interview-style example.


πŸ”Ή Java Program (Bank Example)

// 1. Abstraction (interface)
interface BankOperations {
    void deposit(double amount);
    void withdraw(double amount);
    double getBalance();
}

// 2. Encapsulation (private data + getters/setters)
class BankAccount implements BankOperations {
    private String accountHolder;  // data hidden
    private double balance;        // encapsulated

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

    // Encapsulation β†’ controlled access
    public String getAccountHolder() {
        return accountHolder;
    }

    @Override
    public void deposit(double amount) {
        balance += amount;
        System.out.println(amount + " deposited. Current balance = " + balance);
    }

    @Override
    public void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            System.out.println(amount + " withdrawn. Current balance = " + balance);
        } else {
            System.out.println("Insufficient funds!");
        }
    }

    @Override
    public double getBalance() {
        return balance;
    }
}

// 3. Inheritance (IS-A relationship)
class SavingsAccount extends BankAccount {
    private double interestRate;

    public SavingsAccount(String accountHolder, double initialBalance, double interestRate) {
        super(accountHolder, initialBalance); // reusing parent constructor
        this.interestRate = interestRate;
    }

    // 4. Polymorphism (Method Overriding β†’ Runtime)
    @Override
    public void deposit(double amount) {
        double interest = amount * interestRate / 100;
        super.deposit(amount + interest);
        System.out.println("Interest of " + interest + " added!");
    }
}

// Test Class
public class OOPsDemo {
    public static void main(String[] args) {
        // Polymorphism β†’ Parent reference, Child object
        BankOperations account = new SavingsAccount("Rahul", 1000, 5);

        System.out.println("Account Holder: " + ((SavingsAccount) account).getAccountHolder());
        account.deposit(1000);   // Overridden method (runtime polymorphism)
        account.withdraw(500);   // Inherited method
        System.out.println("Final Balance: " + account.getBalance());
    }
}

πŸ”Ή How OOPs Concepts are Shown Here

  1. Encapsulation β†’ private balance, accessed only via methods.

  2. Abstraction β†’ BankOperations interface hides implementation.

  3. Inheritance β†’ SavingsAccount extends BankAccount.

  4. Polymorphism β†’

    • Overriding: deposit() in SavingsAccount.

    • Runtime binding: BankOperations account = new SavingsAccount(...).


βœ… This one program covers all OOPs pillars, and you can explain it confidently in interviews.

Perfect πŸ‘ Here’s a short and smart interview-style answer template using the Bank example. You can memorize and adapt it when asked:


πŸ”Ή Interview Answer for OOPs Concepts (using Bank Example)

1. Encapsulation
πŸ‘‰ In my program, I encapsulated the account balance by keeping it private inside the BankAccount class. It can only be accessed and modified through public methods like deposit(), withdraw(), and getBalance(). This hides data and ensures controlled access.

2. Abstraction
πŸ‘‰ I used a BankOperations interface that declares methods like deposit(), withdraw(), and getBalance(). The interface hides implementation details and only exposes what operations a bank account should support. Different accounts can have different implementations.

3. Inheritance (IS-A relationship)
πŸ‘‰ The SavingsAccount class extends BankAccount, meaning a SavingsAccount is a BankAccount. It reuses the parent’s functionality and adds extra features like interest.

4. Polymorphism
πŸ‘‰ I demonstrated polymorphism in two ways:

  • Method Overriding (Runtime Polymorphism): In SavingsAccount, I overrode the deposit() method to add interest before depositing.

  • Dynamic Method Dispatch: I used a parent reference BankOperations account = new SavingsAccount(...), and at runtime, the child’s version of deposit() was called.


πŸ”Ή Final Wrap-Up (30 sec summary)

β€œIn short, my program shows Encapsulation by hiding balance with private fields, Abstraction by defining operations in an interface, Inheritance with SavingsAccount extending BankAccount, and Polymorphism with overridden deposit methods and dynamic method dispatch. Together, this covers all four OOP pillars in Java.”