Introduction to OOP
Object-oriented programming (OOP) organises a program around objects — things like a Book, a Cart or a Customer — each holding its own data and knowing how to do its own jobs.
On this page · 6 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 6 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
In simple words#
OOP organises a program like a company: each department (object) owns its own data and does its own job, and departments work together by sending requests to each other.
- Object
- A thing with data (state) and actions (behaviour).
- Class
- The template used to create objects.
- 4 pillars
- Encapsulation, abstraction, inheritance, polymorphism.
Learning objectives#
- Explain what an object is: state plus behaviour
- Name the four pillars of OOP
- Explain why OOP helps in large codebases
- Recognise OOP in JavaScript and C# code
Explanation#
An object bundles state (data such as title and isBorrowed) with behaviour (methods such as borrow()). The object is responsible for keeping its own data correct.
A class is the template used to create objects. You write the class once and create as many objects from it as you need.
OOP rests on four pillars — remember them as A PIE: Abstraction (show only what matters), Polymorphism (one call, many behaviours), Inheritance (reuse a parent’s features) and Encapsulation (protect internal data).
Why bother? In a 100,000-line app, OOP lets each part own one concept. A bug in book borrowing is in the Book class — not scattered across the codebase.
C#, Java, TypeScript, Python and JavaScript all support OOP. Frameworks such as ASP.NET Core, Angular and NestJS are built around classes and objects.
Syntax#
// The same idea in C#
public class Book
{
public string Title { get; }
public bool IsBorrowed { get; private set; }
public Book(string title) => Title = title;
public string Borrow()
{
if (IsBorrowed) return quot;{Title} is already borrowed";
IsBorrowed = true;
return quot;You borrowed {Title}";
}
}
Code example: A tiny library modelled as objects#
class Book {
constructor(title, author) {
this.title = title; // state
this.author = author;
this.isBorrowed = false;
}
borrow() { // behaviour
if (this.isBorrowed) return `${this.title} is already borrowed`;
this.isBorrowed = true;
return `You borrowed ${this.title}`;
}
}
const book1 = new Book('Wings of Fire', 'A. P. J. Abdul Kalam');
const book2 = new Book('The Guide', 'R. K. Narayan');
console.log(book1.borrow());
console.log(book1.borrow());
console.log(book2.isBorrowed);
console.log(book1);
You borrowed Wings of Fire
Wings of Fire is already borrowed
false
{ title: 'Wings of Fire', author: 'A. P. J. Abdul Kalam', isBorrowed: true }Step-by-step explanation#
- class Book is the template: every book has a title, an author and a borrowed flag.
- borrow() is behaviour that belongs to the book; it also enforces the rule “you cannot borrow a borrowed book”.
- book1 and book2 are two separate objects created from the same class.
- Borrowing book1 changes only book1; book2 is still available.
- Logging book1 shows its current state.
Real-world use case#
A library management system has classes for Book, Member, Loan and Fine. Each class owns its rules — a Loan knows its due date, a Fine knows how to calculate itself — so the system stays understandable as it grows.
Common mistakes#
A small utility function does not need a class. OOP is a way to organise behaviour around data, not a rule to wrap every line in a class.
If all the logic lives in functions outside the object, you have lost the main benefit. Put the rules next to the data they protect.
Interviewers ask for examples. For each pillar, be ready with one real example from code you have written.
Best practices#
- Model classes on real concepts in your domain (Order, Invoice)
- Give each object the methods that change its own data
- Keep each class focused on one idea
- Learn the four pillars with one example each
Remember this#
Object = data (state) + methods (behaviour).
The 4 pillars = A PIE: Abstraction, Polymorphism, Inheritance, Encapsulation.
Class = template. Object = one thing made from it.
Practice exercise#
Your task: Model a LightBulb with a state isOn and two methods: toggle() (switches state) and status() (returns "ON" or "OFF"). Toggle it twice and log the status each time.
Need a hint?
Show solution
class LightBulb {
constructor() { this.isOn = false; }
toggle() { this.isOn = !this.isOn; }
status() { return this.isOn ? 'ON' : 'OFF'; }
}
const bulb = new LightBulb();
bulb.toggle();
console.log(bulb.status()); // ON
bulb.toggle();
console.log(bulb.status()); // OFF
Mini quiz#
An object combines…
Want more? Practise all OOP Concepts MCQs · OOP Concepts interview questions
My notes#
Saved in this browser.