Chapter 01 of 26

Introduction to OOP

Think about a food delivery app like Swiggy or Zomato. There are customers, restaurants, menu items, orders, delivery partners and payments. Each of these "things" has some data (a customer has a name and an address, an order has items and a total) and can do something or have something done to it (an order can be placed, cancelled or delivered).

Object-Oriented Programming, or OOP, is a way of writing programs that follows exactly this way of thinking. Instead of writing one long list of instructions, we model the program as a set of objects that hold data and talk to each other. In this chapter we will understand what OOP is, where it came from, its four pillars, and why almost every placement interview asks about it.


What Is Object-Oriented Programming?

Object-Oriented Programming is a programming paradigm in which a program is organised around objects. Each object bundles together some data (its state) and the operations that work on that data (its behaviour), and objects cooperate by calling each other's methods.

A paradigm simply means a style or approach of programming. OOP is one paradigm. Others include procedural programming (C, Pascal) and functional programming (Haskell, and functional features in Java, Python and JavaScript).

The key idea is very simple:

Keep the data and the code that works on that data together, in one unit.

In a procedural program, data lives in variables and arrays, and separate functions operate on it. In an object-oriented program, a BankAccount object holds its own balance and knows how to deposit and withdraw. We will compare both styles in detail in the next chapter, Procedural vs Object-Oriented Programming.


Objects and Classes in One Minute

Object

An object is a thing in your program that has:

  • State: the data it holds right now (stored in fields or attributes).

  • Behaviour: what it can do (written as methods).

  • Identity: something that makes it a separate object, even if another object has exactly the same data.

Object

State (data)

Behaviour (methods)

Bank account

account number, owner, balance

deposit, withdraw, check balance

Food order

items, total, status

place, cancel, mark delivered

Student

roll number, name, marks

calculate CGPA, register for course

Car

brand, colour, current speed

accelerate, brake, turn

Class

A class is the blueprint or template from which objects are made. It says which data every object of that kind will have and which methods it will support.

        Class: BankAccount            (blueprint, written once)
        fields : owner, balance
        methods: deposit(), withdraw()
                 |
        +--------+---------+
        |                  |
        v                  v
   Object riya        Object arjun       (instances, created many times)
   owner  = "Riya"    owner  = "Arjun"
   balance = 5000     balance = 1000

One class, many objects. Each object has its own copy of the data, but all of them share the same methods. The chapter Classes & Objects covers this in depth.


Your First Taste of OOP in Java

Here is a small but complete Java program. Read it once; every line will be explained in later chapters.

class BankAccount {
    String owner;
    double balance;

    BankAccount(String owner, double balance) {
        this.owner = owner;
        this.balance = balance;
    }

    void deposit(double amount) {
        balance = balance + amount;
        System.out.println(owner + " deposited " + amount + ", balance = " + balance);
    }

    void withdraw(double amount) {
        if (amount > balance) {
            System.out.println(owner + ": insufficient balance!");
            return;
        }
        balance = balance - amount;
        System.out.println(owner + " withdrew " + amount + ", balance = " + balance);
    }
}

class Main {
    public static void main(String[] args) {
        BankAccount riya = new BankAccount("Riya", 5000);
        BankAccount arjun = new BankAccount("Arjun", 1000);

        riya.deposit(2000);
        arjun.withdraw(1500);
        arjun.deposit(700);
        arjun.withdraw(1500);
    }
}

Output:

Riya deposited 2000.0, balance = 7000.0
Arjun: insufficient balance!
Arjun deposited 700.0, balance = 1700.0
Arjun withdrew 1500.0, balance = 200.0

Notice a few things:

  • We wrote the deposit and withdraw logic once, inside the class, but used it on two different accounts.

  • Each object remembers its own balance. Arjun's failed withdrawal did not affect Riya's money.

  • The rule "you cannot withdraw more than your balance" lives inside the account. The code in main does not need to know it.

  • We call a method on an object with the dot operator: riya.deposit(2000). In OOP language this is often described as sending a message to the object: "Riya's account, please deposit 2000."

There is still a weakness in this version: main could simply write arjun.balance = -99999; and nothing would stop it. Fixing that is the job of encapsulation, which we will meet in a moment.


The Four Pillars of OOP

Almost every textbook and interviewer describes OOP using four core principles, often called the four pillars.

1. Encapsulation

Encapsulation means bundling data and the methods that operate on it into one unit (a class), and hiding the internal data so it can only be changed through those methods.

Real-life example: an ATM. You cannot open the machine and take out notes; you can only use the screen and keypad, and the machine checks your PIN and balance before giving cash. Covered in the chapter Encapsulation.

2. Abstraction

Abstraction means showing only the essential features of something and hiding the unnecessary details of how it works.

Real-life example: you drive a car using the steering wheel, accelerator and brake. You do not need to know how fuel injection or the gearbox works. Covered in the chapter Abstraction.

3. Inheritance

Inheritance lets a new class acquire the fields and methods of an existing class, so common code is written once and reused. It models an "IS-A" relationship.

Real-life example: a SavingsAccount IS-A BankAccount, with extra features like interest. Covered in the chapters Inheritance and Types of Inheritance.

4. Polymorphism

Polymorphism ("many forms") means the same method call can behave differently depending on the actual object it is called on.

Real-life example: you say "pay" to a payment app. If the method is UPI, it opens your UPI app; if it is a card, it asks for the card details. One request, different behaviour. Covered in the chapter Polymorphism.

All Four Pillars in One Program

Do not worry about keywords like abstract, extends and @Override yet. Just look at the comments to see where each pillar appears.

abstract class Shape {                       // abstraction: "some shape"
    private final String name;               // encapsulation: hidden data

    Shape(String name) { this.name = name; }

    String getName() { return name; }

    abstract double area();                  // every shape must know its area
}

class Circle extends Shape {                 // inheritance: a Circle IS-A Shape
    private final double radius;

    Circle(double radius) {
        super("Circle");
        this.radius = radius;
    }

    @Override
    double area() { return Math.PI * radius * radius; }
}

class Rectangle extends Shape {
    private final double length, width;

    Rectangle(double length, double width) {
        super("Rectangle");
        this.length = length;
        this.width = width;
    }

    @Override
    double area() { return length * width; }
}

class Main {
    public static void main(String[] args) {
        Shape[] shapes = { new Circle(1.5), new Rectangle(4, 2.5) };

        for (Shape s : shapes) {             // polymorphism: one call, many forms
            System.out.printf("%s area = %.2f%n", s.getName(), s.area());
        }
    }
}

Output:

Circle area = 7.07
Rectangle area = 10.00

The loop in main calls s.area() without knowing whether s is a circle or a rectangle. Tomorrow we could add a Triangle class and this loop would work without a single change. That flexibility is one of the main reasons OOP is used for large software.

Pillar

One-line meaning

Main benefit

Encapsulation

Bundle data + methods, hide the data

Protects objects from invalid changes

Abstraction

Show what, hide how

Reduces complexity for the user of a class

Inheritance

Child class reuses parent class

Code reuse, IS-A hierarchies

Polymorphism

One interface, many implementations

Code that works with types not yet written

Note: Some books list only three pillars (encapsulation, inheritance, polymorphism) and treat abstraction as part of encapsulation. Most interviews in India expect all four.


Other Important OOP Terms

You will meet these words again and again in this tutorial:

  • Instance: another word for an object. "riya is an instance of BankAccount."

  • Field / attribute / data member: a variable that belongs to an object.

  • Method / member function: a function that belongs to a class.

  • Message passing: objects communicate by calling each other's methods.

  • Dynamic binding: deciding at run time which method body to execute (what made s.area() work above).

  • Constructor: special code that runs when an object is created. See Constructors & Destructors.

  • Association, aggregation, composition: ways in which objects are related to each other ("an order HAS items"). See Association, Aggregation & Composition.


A Short History of OOP

Year

Milestone

1967

Simula 67, created by Ole-Johan Dahl and Kristen Nygaard in Norway for simulations, introduces classes, objects, inheritance and virtual procedures. It is regarded as the first object-oriented language.

1970s

Smalltalk is developed at Xerox PARC by Alan Kay's group. Kay is credited with coining the term "object-oriented programming". In Smalltalk, everything is an object.

1979–1985

Bjarne Stroustrup starts "C with Classes" at Bell Labs; it is renamed C++ in 1983 and released commercially in 1985.

1991

Python is first released by Guido van Rossum.

1995

Java is released by Sun Microsystems (James Gosling's team), making OOP mainstream for enterprise and later Android development.

2000s

C# (Microsoft), and later Kotlin and Swift, carry OOP forward, mixed with functional features.

Today most popular languages are multi-paradigm: they support OOP but also procedural and functional styles.


Is Java a Pure Object-Oriented Language?

This is a classic interview question. A pure object-oriented language is one in which everything is an object and all work is done by objects sending messages to each other. Smalltalk is the standard example; Ruby is also very close, since even 5 is an object there.

Java is not purely object-oriented, mainly because:

  • It has primitive types (int, double, char, boolean, ...) which are not objects.

  • It has static members, which belong to the class and can be used without creating any object (Math.sqrt(16)).

It is, however, strongly object-oriented: all code must live inside a class, and there are wrapper classes like Integer for primitives.

Language

OOP nature

Smalltalk

Pure OOP

Java

Object-oriented, but not pure (primitives, static)

C++

Multi-paradigm; you can write a full program with no classes at all

Python

Every value (even 5 or a function) is an object, but you are free to write plain procedural scripts

C

Not object-oriented (procedural)


Advantages and Disadvantages of OOP

Advantages

  • Modularity: each class is a separate, understandable piece. Teams can work on different classes in parallel.

  • Reusability: classes can be reused in other programs, and inheritance and composition reuse existing code.

  • Data protection: encapsulation keeps objects in a valid state.

  • Easier maintenance: a change in how something works is usually made in one class.

  • Extensibility: polymorphism lets you add new types without changing existing code.

  • Natural modelling: software objects map well to real-world entities, which makes design discussions easier.

Disadvantages

  • More upfront design: deciding the right classes takes thought; a poor class design is hard to fix later.

  • Can be overkill: a 30-line script does not need five classes.

  • Some overhead: objects use extra memory (object headers, references), and dynamic method calls can be slightly slower, though modern compilers and JITs reduce this a lot.

  • Risk of over-engineering: deep inheritance trees and too many layers make code harder, not easier, to follow.


Common Mistakes Students Make

  • Thinking OOP means Java. OOP is a way of thinking; Java, C++, Python, C# and Kotlin are just languages that support it.

  • Confusing class and object. The class is the blueprint; objects are the real things made from it. "Student" is a class; "Aditi with roll number 21" is an object.

  • Believing "I used a class, so my code is OOP". A class with only static methods and public data is still procedural code in disguise.

  • Using inheritance everywhere. Inheritance is powerful but often misused. Many designs are better with composition, as we will see later.

  • Mixing up encapsulation and abstraction. Encapsulation is about hiding and protecting data; abstraction is about hiding complexity and showing only what matters.


Interview Questions

1. What is OOP?

OOP is a programming paradigm that organises software as a collection of objects. Each object combines state (data) and behaviour (methods), and objects work together by calling each other's methods. Its main principles are encapsulation, abstraction, inheritance and polymorphism.

2. What are the four pillars of OOP? Give a one-line example of each.

Encapsulation (an account's balance can change only through deposit/withdraw), abstraction (you use a car's steering without knowing its internals), inheritance (SavingsAccount extends BankAccount), and polymorphism (shape.area() computes differently for a circle and a rectangle).

3. What is the difference between a class and an object?

A class is a blueprint that defines fields and methods. An object is a concrete instance of a class, created at run time, with its own values for those fields. A class is defined once; many objects can be created from it.

4. Which was the first object-oriented language?

Simula 67, created by Ole-Johan Dahl and Kristen Nygaard. Smalltalk, developed later at Xerox PARC, popularised the term "object-oriented programming".

5. Is Java 100% object-oriented? Why or why not?

No. Java has primitive types (int, char, etc.) that are not objects, and static members that can be used without any object. Everything else, including all code, must be inside classes, so Java is strongly but not purely object-oriented.

6. Which of these is NOT a feature of OOP: (a) inheritance (b) encapsulation (c) polymorphism (d) top-down design?

(d). Top-down design (breaking a problem into smaller procedures) is the classic approach of procedural programming. OOP design is usually described as bottom-up: you design the objects first and then combine them.

7. What is the identity of an object?

Identity is what makes an object distinct from every other object, even one with identical state. In Java, two new BankAccount("Riya", 5000) objects have equal data but different identities, so a == b is false.


Summary

  • OOP organises a program around objects that bundle state and behaviour.

  • A class is a blueprint; an object is an instance of that blueprint with state, behaviour and identity.

  • The four pillars are encapsulation, abstraction, inheritance and polymorphism.

  • OOP began with Simula 67, was popularised by Smalltalk, and went mainstream with C++ and Java.

  • Java is strongly object-oriented but not pure, because of primitive types and static members.

  • OOP brings modularity, reuse, protection and extensibility, at the cost of more upfront design and some overhead.

In the next chapter, Procedural vs Object-Oriented Programming, we will write the same program in both styles and see exactly what problems OOP solves.