Resources / CS fundamentals
Free to readCS fundamentals⏱ 8 min read

OOP concepts with real examples (interview-ready)

The 4 pillars, SOLID in one line each, and the 10 OOP questions asked most.

How to use this guide

OOP (object-oriented programming) is asked in almost every fresher technical interview. Learn the idea, see it in a real app, read the code and its output, then practise the questions.

1. Class and object

📖 Class and object

Definition: A class is a user-defined blueprint that describes the data (fields/attributes) and behaviour (methods) of a type. An object is a concrete instance of a class created at run time, occupying memory and holding its own values.

How it works: Defining a class creates no data. new Order(...) allocates memory for one object, runs the constructor to initialise it, and returns a reference to it. Many objects can be created from one class, each with its own state.

Example: Order is the class; your order #4521 (Biryani, ₹250) and someone else's #4522 are two objects of it.

📖 Constructor

Definition: A constructor is a special method that runs automatically when an object is created, used to initialise the object's fields.

How it works: It has the same name as the class (Java) or is __init__ (Python), has no return type, and can be overloaded. If you write none, Java adds a default no-argument constructor.

Example: new Student("Asha", 8.4) calls the constructor that sets name and CGPA.

🧒 In simple words

A class is a blueprint; an object is a thing built from it.

🌍 Real-life example

"Swiggy order" is a class. Your order #4521 for biryani is an object with its own items, amount and status.

Java
class Order {
    String item; int amount;
    Order(String item, int amount) { this.item = item; this.amount = amount; }
    void show() { System.out.println(item + " - Rs " + amount); }
}
public class Main {
    public static void main(String[] a) {
        new Order("Biryani", 250).show();
        new Order("Pizza", 400).show();
    }
}
▶ Output
Biryani - Rs 250
Pizza - Rs 400

2. Encapsulation — protect the data

📖 Encapsulation

Definition: Encapsulation is the OOP principle of bundling an object's data (fields) and the methods that work on that data into one unit (a class), and restricting direct access to the data from outside.

How it works: Fields are declared private, so other classes can't read or change them directly. Access goes through public methods (getters/setters or business methods like withdraw()), which can validate input and keep the object in a correct state. This is also called data hiding.

Example: A bank account's balance is private; the only way to change it is deposit() or withdraw(), and withdraw() refuses to go below zero.

Java
class Account {
    private int balance;               // hidden
    public void deposit(int amt) { if (amt > 0) balance += amt; }
    public boolean withdraw(int amt) { if (amt > balance) return false; balance -= amt; return true; }
    public int getBalance() { return balance; }
}

🧒 In simple words

Keep fields private and change them only through methods that check the rules.

🌍 Real-life example

Your bank balance can't be set directly to ₹1 crore — the bank only allows deposit() and withdraw(), and withdraw() checks you have enough money.

Java
class Account {
    private int balance = 1000;
    boolean withdraw(int amt) {
        if (amt > balance) return false;
        balance -= amt; return true;
    }
    int getBalance() { return balance; }
}
// in main:
Account ac = new Account();
System.out.println(ac.withdraw(1500) + " " + ac.getBalance());
System.out.println(ac.withdraw(400) + " " + ac.getBalance());
▶ Output
false 1000
true 600

3. Inheritance — reuse

📖 Inheritance

Definition: Inheritance is the mechanism by which one class (the child, subclass or derived class) acquires the fields and methods of another class (the parent, superclass or base class), creating an is-a relationship.

How it works: The child declares extends Parent (Java) or class Child(Parent) (Python). It reuses the parent's code, can add new members, and can override parent methods to change behaviour. Java supports single inheritance of classes (one parent) but a class can implement many interfaces. Types: single, multilevel, hierarchical (and multiple via interfaces).

Example: Car extends Vehicle — every Car is a Vehicle, so it gets start() from Vehicle and adds its own openSunroof().

🧒 In simple words

A child class gets the parent's fields and methods and can add its own.

🌍 Real-life example

Every vehicle has a number and can start; a Car adds AC, a Bike adds kick-start.

Java
class Vehicle { void start() { System.out.println("Starting"); } }
class Car extends Vehicle { void ac() { System.out.println("AC on"); } }
// main:
Car c = new Car(); c.start(); c.ac();
▶ Output
Starting
AC on

4. Polymorphism — one name, many forms

📖 Polymorphism

Definition: Polymorphism (Greek: "many forms") is the ability of one interface — a method name or a reference type — to behave differently depending on the actual object or the arguments used.

How it works: Compile-time (static) polymorphism is method overloading: same method name, different parameter lists; the compiler picks the version. Run-time (dynamic) polymorphism is method overriding: a child class replaces a parent method, and the JVM decides which version to run based on the actual object type (dynamic method dispatch).

Example: A Payment reference can point to a Upi or Card object; calling pay() runs the UPI or card version automatically.

Java
Payment p = new Upi();  p.pay(500);   // UPI version
p = new Card();         p.pay(500);   // Card version
▶ Output
UPI paid 500
Card paid 500 + 2% fee

🧒 In simple words

TypeWhen decidedExample
Overloadingcompile timeadd(int, int) and add(double, double)
Overridingrun timeeach payment type's pay()
Java
abstract class Payment { abstract void pay(int amt); }
class Upi extends Payment { void pay(int a) { System.out.println("UPI paid " + a); } }
class Card extends Payment { void pay(int a) { System.out.println("Card paid " + a + " + 2% fee"); } }
// main:
Payment[] ps = { new Upi(), new Card() };
for (Payment p : ps) p.pay(500);
▶ Output
UPI paid 500
Card paid 500 + 2% fee
🌍 Real-life example

At checkout the app just calls pay() — it doesn't care whether you chose UPI or card. Each one does its own thing.

5. Abstraction — show what, hide how

📖 Abstraction

Definition: Abstraction is the principle of exposing only the essential features of an object (what it does) while hiding the implementation details (how it does it).

How it works: In Java it's achieved with abstract classes (can have both abstract and concrete methods, constructors and state) and interfaces (a contract of methods a class promises to implement). Callers depend on the abstract type, so the implementation can change without breaking them.

Example: You call bookCab(pickup, drop); the matching of drivers, pricing and routing are hidden behind that one method.

🧒 In simple words

You press "Book cab"; you don't see the matching algorithm. In code: abstract classes and interfaces.

Abstract classInterface
Methodsabstract + normalabstract (+ default methods since Java 8)
Fieldsanyconstants only
A class can useonemany

6. SOLID in one line each

LetterPrincipleOne line
SSingle responsibilitya class does one job
OOpen/closedadd new features without editing old code
LLiskov substitutiona child must work wherever the parent works
IInterface segregationsmall focused interfaces, not one giant one
DDependency inversiondepend on interfaces, not concrete classes

7. Interview questions

What is the difference between a class and an object?

Show answer

A class is the blueprint (defines fields and methods); an object is a real instance in memory with its own values. One class → many objects.

Can we override a static method?

Show answer

No. Static methods belong to the class, not the object. A child can declare a method with the same signature, but that hides the parent's method — it isn't overriding (no run-time polymorphism).

Why doesn't Java support multiple inheritance of classes?

Show answer

To avoid the "diamond problem" — if two parents have the same method, which one does the child get? Java allows multiple interfaces instead.

What is a constructor? Can it be private?

Show answer

A special method that runs when an object is created, used to set initial values. Yes, it can be private — used in the Singleton pattern so only one object can be created.

What is the difference between composition and inheritance?

Show answer

Inheritance is "is-a" (Car is a Vehicle). Composition is "has-a" (Car has an Engine). Prefer composition when you only need to reuse behaviour — it's more flexible.

8. Your 7-day OOP plan

🗓 7 days
DayDo
1Classes, objects, constructors — write 3 classes
2Encapsulation — bank account with rules
3Inheritance — vehicle hierarchy
4Polymorphism — payment system
5Abstraction, interfaces
6SOLID + design a parking lot on paper
7Answer all interview questions out loud

Further references

🔥Join the free 30-Day Placement ChallengeOne resource and one task a day — from resume to offer.