What is Object-Oriented Programming (OOP)?

 

What is Object-Oriented Programming (OOP)?

A Complete Interview-Oriented Guide with Real-World Examples (Java)


✅ Introduction

Object-Oriented Programming (OOP) is one of the most important programming paradigms used in enterprise applications, backend systems, game engines, mobile apps, financial platforms, and modern software design.

In simple words:

OOP is a programming approach where software is designed using objects.
Each object contains:

  • State (Data) → variables / attributes

  • Behavior (Actions) → methods / functions

Think of a “Car”:

  • State → color, model, speed

  • Behavior → start(), stop(), accelerate()

That’s OOP!


🔥 Why OOP?

✔ Better code reusability
✔ Cleaner and modular structure
✔ Easier to maintain and extend
✔ Closer to real-world modelling
✔ Supports scalability

No wonder Java, C++, Python, C#, Kotlin, Swift, etc., are heavily OOP-driven.


🧱 Core Concepts of OOP (4 Pillars)

Every interviewer expects clear understanding of:
1️⃣ Encapsulation
2️⃣ Inheritance
3️⃣ Polymorphism
4️⃣ Abstraction

Let’s understand each with simple language + Java code + interview clarity.


🔒 1) Encapsulation — Data Protection + Controlled Access

Definition (Interview Answer):
Encapsulation is wrapping data and methods together inside a class and restricting direct access to the data using access modifiers like private, public, etc.
Data is accessed through getters & setters.

🏦 Real-World Example

A bank does not allow directly changing balance.
You must deposit() / withdraw() through proper rules.

✅ Java Example

class BankAccount { private double balance; // data hidden public double getBalance() { return balance; } public void deposit(double amount) { if (amount <= 0) { System.out.println("Invalid deposit"); return; } balance += amount; System.out.println("Deposited: " + amount); } }

✔ Data security
✔ Validation
✔ Controlled modification

Interview Tip:
Encapsulation supports data hiding, maintainability, and security.


🧬 2) Inheritance — Code Reusability + IS-A Relationship

Definition (Interview Answer):
Inheritance allows one class (child/subclass) to acquire properties and behaviors of another class (parent/superclass).
This represents IS-A relationship.

Example:

  • Car IS-A Vehicle

  • Dog IS-A Animal

🚗 Java Example

class Vehicle { void drive() { System.out.println("Vehicle is moving"); } } class Car extends Vehicle { void playMusic() { System.out.println("Playing music in car"); } }

Output

Vehicle is moving Playing music in car

✔ Code reuse
✔ Logical hierarchy
✔ Reduces duplication


🎭 3) Polymorphism — Same Action, Different Behavior

Definition (Interview Answer):
Polymorphism means one name, many forms.
In Java, it occurs in two ways:

✔ Compile-Time Polymorphism (Method Overloading)

Same method name, different parameters.

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

✔ Runtime Polymorphism (Method Overriding)

Child class provides its own implementation of a parent method.

class Payment { void pay(double amount) { System.out.println("General Payment"); } } class UpiPayment extends Payment { void pay(double amount) { System.out.println("Paying using UPI"); } }

Key Interview Line:
Runtime polymorphism works using dynamic binding where the decision is made at runtime based on object type.


🧩 4) Abstraction — Hide Internal Complexity

Definition (Interview Answer):
Abstraction means hiding internal implementation and exposing only essential functionality.

We achieve abstraction using:
✔ Abstract Classes
✔ Interfaces

🔔 Real-World Example

When you use a Washing Machine:

  • You press Start()

  • You don't know internal motor logic
    That is abstraction.


✅ Java Example — Interface Based

interface Notification { void send(String message); } class EmailNotification implements Notification { public void send(String message) { System.out.println("Email Sent: " + message); } }

Caller only calls send() — internal working is hidden.


🏢 Real-World Style Example — Food Delivery Mini-System

This simple mini-example shows multiple OOP concepts together:

✔ Encapsulation → order data protected
✔ Polymorphism → multiple payment modes
✔ Abstraction → notification channel hidden
✔ Composition → Restaurant HAS-A Menu

abstract class PaymentMethod { abstract void pay(double amount); } class CardPayment extends PaymentMethod { void pay(double amount) { System.out.println("Paid using Card: " + amount); } } interface Notification { void send(String msg); } class EmailNotification implements Notification { public void send(String msg) { System.out.println("EMAIL: " + msg); } } class Order { private double amount; public Order(double amount) { this.amount = amount; } public double getAmount() { return amount; } }

🎯 OOP Interview Q&A — Quick Punch Answers


❓ What is OOP?

Programming style based on objects that combine data and behavior.


❓ Why OOP?

  • Reusability

  • Maintainability

  • Real-world modelling

  • Security

  • Scalability


❓ What are the 4 pillars of OOP?

Encapsulation, Inheritance, Polymorphism, Abstraction


❓ Difference between Abstraction and Encapsulation?

AbstractionEncapsulation
Hides implementationHides data
Focus on “what” system doesFocus on “how” data is accessed
Done using abstract classes & interfacesDone using private variables + getters/setters

❓ What is IS-A vs HAS-A relationship?

IS-A (Inheritance)
Car IS-A Vehicle

HAS-A (Composition)
Car HAS-A Engine


❓ What is Runtime Polymorphism?

When method call is decided at runtime using overriding.


❓ Can static methods be overridden?

No. They are hidden, not overridden.


🏁 Final Summary

Object-Oriented Programming helps developers build structured, modular, and real-world like applications.

  • Encapsulation → protects data

  • Inheritance → reuses behavior

  • Polymorphism → flexibility

  • Abstraction → hides complexity

No comments:

Post a Comment

System Design + Java Program for Tic-Tac-Toe (TicTac)

  🎮 System Design + Java Program for Tic-Tac-Toe (TicTac) In this mini system design, we’ll build a console-based Tic-Tac-Toe game in Java...

Featured Posts