OOP Concepts · Foundations

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.

Beginner 6 min Lesson 1 of 38 Runnable examplesExamples use modern JavaScript (ES2022 private fields) and C# 13.
0/38 in OOP Concepts
On this page · 6 min · Beginner

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#

C# Illustrative
// 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#

JavaScriptA tiny library modelled as objects Runnable
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);
Expected output
You borrowed Wings of Fire
Wings of Fire is already borrowed
false
{ title: 'Wings of Fire', author: 'A. P. J. Abdul Kalam', isBorrowed: true }
Opens this example in the full playground

Step-by-step explanation#

  1. class Book is the template: every book has a title, an author and a borrowed flag.
  2. borrow() is behaviour that belongs to the book; it also enforces the rule “you cannot borrow a borrowed book”.
  3. book1 and book2 are two separate objects created from the same class.
  4. Borrowing book1 changes only book1; book2 is still available.
  5. 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#

Thinking OOP means “use classes for everything”

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.

Objects that only hold data

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.

Learning the pillars as definitions only

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?
toggle can flip the flag with this.isOn = !this.isOn.
Show solution
JavaScript Runnable
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
Opens this example in the full playground

Mini quiz#

Introduction to OOP quizMultiple choice Question 1 of 3

An object combines…

Score so far: 0/0

Want more? Practise all OOP Concepts MCQs · OOP Concepts interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.