What is object-oriented programming?
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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.
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.
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.
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)
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.
Next step
Check your understanding
10 quick questions