Skip to lesson

What is object-oriented programming?

Java is an object-oriented language: a Java program is built from objects that work together, much like people and things do in real life.

What OOP means

Object-oriented programming (OOP) is a way of designing a program as a set of objects that work together. Each object holds its own data and the actions that use that data.

Look at your phone. It knows things: its brand, its battery level, its contacts. It does things: it makes calls, charges and takes photos.

OOP builds a program from such things, called objects. Each object keeps its data and its actions together in one place.

A real mobile phone seen as an object. What it knows (its data): brand Redmi, battery 80% and 245 contacts. What it does (its actions): make a call, charge and take a photo. One object keeps its own data and the actions that use that data together.

State, behaviour and identity

An object is one real thing in a program that has state (the data it holds), behaviour (the actions it can do) and identity (what makes it different from every other object).

A student, a bank account and a cricket player can each be described in the same three parts.

  • State is the data an object holds right now: a student's marks, an account's balance. State can change: the balance goes down after a withdrawal.
  • Behaviour is what an object can do: a student writes exams, an account accepts a deposit, a player bats.
  • Identity is what makes one object different from all others, like a roll number or an account number. Two students with the same name and marks are still two different students.
Three everyday objects. A student (roll no. 21): state name Priya, marks 91; behaviour writes exams, pays fees. A bank account (no. 4521): state owner Rohan, balance Rs 500; behaviour deposit, withdraw. A cricket player (jersey 18): state name Virat, runs 82; behaviour bats, takes a catch.

Class and object

A class is a blueprint (template) that describes the data and actions one kind of thing will have. An object is one real thing made from a class, with its own values; it is also called an instance of that class.

A builder draws one house plan and builds many houses from it. The plan says every house has an owner and some rooms; each real house has its own.

In a school app, Student is the class. Aarav, Priya and Kavya are three Student objects.

A class is like a house plan: drawn once, it says every house has an owner and a number of rooms. Three real houses, the objects, are built from the one plan: Ravi's house with 2 rooms, Meena's with 3 and Arjun's with 2. Each house has its own values.
A class is made once; you can make as many objects from it as you need. Priya's marks belong to the Priya object, not to the Student class.

How objects work together

Message passing is how objects work together: one object asks another object to do one of its actions, and that object does it with its own data.

Priya wants Rs 500 from an ATM. She asks the ATM, the ATM asks her bank account, and the account checks its own balance, takes out the money and replies.

Each object does only its own job. Why this beats one long list of steps: Procedural vs object-oriented programming (opens in a new tab)

Three objects work together. Priya, the customer, asks the ATM to withdraw Rs 500. The ATM asks the bank account, which holds Rs 2,000, to take out Rs 500. The account replies done, balance Rs 1,500, and the ATM gives Priya her Rs 500.

The four pillars of OOP

The four pillars of OOP are encapsulation, inheritance, polymorphism and abstraction. They are the four main ideas used to design object-oriented programs.

Each pillar is one idea you already see in daily life. Each one gets its own module later in this section, with code: Encapsulation (opens in a new tab), Inheritance (opens in a new tab), Polymorphism (opens in a new tab) and Abstraction (opens in a new tab).

  • Encapsulation: keeping an object's data and actions together in one unit and protecting the data from outside. An ATM gives cash only through its screen; nobody opens the cash box.
  • Inheritance: one class gets the data and actions of another class and adds its own. A child takes after the parents and still has features of their own.
  • Polymorphism: one action, many forms. The same Share button sends a photo on WhatsApp, attaches it in Gmail or uploads it to Drive.
  • Abstraction: showing only what is needed and hiding how it works inside. You drive a car with the steering, brake and accelerator without knowing how the engine works.
The four pillars of OOP. Encapsulation: data and actions in one unit (an ATM hides its cash box). Inheritance: reuse a parent's features (a child takes after parents). Polymorphism: one action, many forms (one Share button). Abstraction: show what to use, hide how (driving a car).
Java, C++, C# and Python all support OOP, so these ideas carry over to each of them.

Next step

Check your understanding

10 quick questions