What is OOP in Java
Table of Contents + −
In the last lesson you learned about the Java this keyword. But why bother with classes and objects at all? The big idea behind them has a name. It is called object-oriented programming, or OOP for short.
🤔 What problem does OOP solve?
Picture a school program with student data and student actions. Without structure it hurts:
- The data sits in one pile. The actions sit in another. Nothing connects them.
- Change how a student works and you must hunt through the whole program.
- A small change in one place breaks something far away.
OOP fixes this by bundling the data and its actions into one package called an object:
- A
Studentobject holds the data and the actions together. - You change it in one place.
Think of a TV remote. You press a button and the TV reacts. The remote hides the messy circuits behind simple buttons. An object works the same way.
🧩 Procedural code vs object-oriented code
The older style is called procedural programming:
- Data and functions live separately.
- You pass the data into functions by hand every time.
- Nothing says the data and the action belong together.
public class Main { public static void main(String[] args) { // data sits loose in plain variables String name = "Alex"; int age = 20;
// a function that takes the data from outside introduce(name, age); }
static void introduce(String name, int age) { System.out.println("Hi, I am " + name + " and I am " + age); }}Here name, age, and introduce are strangers. You must pass the right values to the right function every time.
Output
Hi, I am Alex and I am 20Now the object-oriented way. The data and the action live inside one class.
public class Main { public static void main(String[] args) { Student alex = new Student("Alex", 20); alex.introduce(); // the object already carries its own data }}
class Student { String name; // data int age; // data
Student(String name, int age) { this.name = name; this.age = age; }
void introduce() { // behavior, right next to the data System.out.println("Hi, I am " + name + " and I am " + age); }}See the shift:
- We no longer pass
nameandageintointroduce. The object already holds them. - We just say
alex.introduce()and the object uses its own data. - Data and action are now one unit that models a real student.
That is the whole point of OOP. Write code that maps to real-world things.
🏛️ The four pillars of OOP
OOP stands on four big ideas. People call them the four pillars. Here is a quick taste of each. Each gets its own lesson next.
Encapsulation: hide data behind methods
Encapsulation keeps an object’s data safe:
- The data is
private. - The outside touches it only through methods.
- The method checks the value first, so nobody sets a bad value.
class BankAccount { private double balance; // hidden from outside
void deposit(double amount) { if (amount > 0) { // the method guards the data balance += amount; } }
double getBalance() { return balance; }}Because balance is private, no code can do account.balance = -500. It must go through deposit, which rejects bad amounts. More in Java Encapsulation.
Inheritance: reuse through parent and child
Inheritance lets one class take on the fields and methods of another:
- The child reuses the parent’s code.
- The child adds its own code on top.
- You do not repeat shared code.
Here Dog borrows eat from Animal and adds its own bark.
class Animal { void eat() { System.out.println("This animal is eating"); }}
class Dog extends Animal { // Dog is a kind of Animal void bark() { System.out.println("Woof!"); }}
public class Main { public static void main(String[] args) { Dog d = new Dog(); d.eat(); // borrowed from Animal d.bark(); // its own method }}The word extends means “Dog is a kind of Animal”. So Dog gets eat without writing it again. More in Java Inheritance.
Output
This animal is eatingWoof!Polymorphism: one interface, many forms
Polymorphism means one method name behaves differently depending on the object:
- You call the same method name.
- Each object responds in its own way.
Here both classes have a sound method, but each does its own thing.
class Animal { void sound() { System.out.println("Some sound"); }}
class Cat extends Animal { void sound() { // same name, different behavior System.out.println("Meow"); }}
public class Main { public static void main(String[] args) { Animal a = new Cat(); // an Animal variable holding a Cat a.sound(); // runs the Cat version }}The variable a has the type Animal, but holds a Cat. So a.sound() runs the Cat version. Same call, different result depending on the real object. More in Java Polymorphism.
Output
MeowAbstraction: show essentials, hide complexity
Abstraction shows only what matters and hides the messy details:
- The user sees simple methods, not the complicated parts inside.
- It is the remote-control idea: simple buttons outside, complex wiring hidden.
Here the user just calls start and never sees how the engine works.
class Car { void start() { ignite(); // hidden detail pumpFuel(); // hidden detail System.out.println("Car started"); }
private void ignite() { // complex stuff hidden away }
private void pumpFuel() { // more complex stuff hidden away }}The driver calls start and the car runs. They never need to know about ignite or pumpFuel. More in Java Abstraction.
Each pillar has its own lesson
This page is only the tour. You just got a one-line idea and a tiny taste of each pillar. Encapsulation, inheritance, polymorphism, and abstraction each get a full lesson next, with many more examples. So if any of these feel fuzzy right now, that is completely fine.
🎯 Why use OOP?
Teams build large programs this way for clear reasons:
- Reusability: write shared code once in a parent class, and every child reuses it.
- Maintainability: data and behavior stay together, so changes stay local. Fix the
Studentclass, and every student updates. - Modeling reality: a
Userclass, anOrderclass, anAccountclass. The code reads like the real problem.
Here is reuse paying off. We make three Student objects from one class. Each carries its own data, but all share the same code.
public class Main { public static void main(String[] args) { Student a = new Student("Alex", 20); Student b = new Student("Riya", 22); Student c = new Student("Sam", 19);
a.introduce(); b.introduce(); c.introduce(); }}
class Student { String name; int age;
Student(String name, int age) { this.name = name; this.age = age; }
void introduce() { System.out.println(name + " is " + age + " years old"); }}One class, three objects. We wrote introduce once, yet each object runs it with its own data.
Output
Alex is 20 years oldRiya is 22 years oldSam is 19 years old⚠️ Common Mistakes
A few wrong ideas trip up beginners:
- Mixing up a class and an object. A class is the blueprint. An object is the real thing built from it.
- Leaving data public instead of protecting it, so any code can set a bad value.
- Forcing everything into deep inheritance. Use it only when the child truly is a kind of the parent.
Student s; // ❌ not an object yet, just a variableStudent s = new Student("Alex", 20); // ✅ new actually builds the object
public double balance; // ❌ anyone can set account.balance = -999private double balance; // ✅ guarded, changed only through methods
class Car extends Engine { } // ❌ a Car is NOT a kind of Engineclass Car { Engine engine; } // ✅ a Car HAS an Engine instead✅ Best Practices
Habits that keep object-oriented code clean:
- Give each class one clear job. A
Studentclass handles students, nothing else. - Keep fields private by default. Let methods control how the data changes.
- Use real-world names like
Order,Account, andUser. - Prefer “has a” over “is a” when unsure. Use inheritance only for a true kind-of.
- Learn the four pillars one at a time. They build on each other.
🧩 What You’ve Learned
Nice work. You now have the big picture of OOP. Let’s recap.
- ✅ OOP organizes code around objects that bundle data and behavior together.
- ✅ Procedural code keeps data and functions separate; OOP keeps them in one unit.
- ✅ OOP stands on four pillars: encapsulation, inheritance, polymorphism, and abstraction.
- ✅ Encapsulation hides data behind methods; inheritance reuses code through parent and child.
- ✅ Polymorphism lets one call behave in many forms; abstraction shows essentials and hides complexity.
- ✅ OOP gives you reusability, maintainability, and code that models the real world.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does object-oriented programming organize code around?
Why: OOP groups related data and the actions on that data into one unit called an object.
- 2
How does procedural code differ from object-oriented code?
Why: In procedural code the data and the functions are strangers; OOP puts them together inside a class.
- 3
Which set lists the four pillars of OOP?
Why: The four pillars are encapsulation, inheritance, polymorphism, and abstraction.
- 4
Which pillar means hiding data behind methods so it stays protected?
Why: Encapsulation keeps fields private and lets the outside world change them only through controlled methods.
🚀 What’s Next?
You now understand the big idea of OOP and the four pillars at a glance. Time to dive into the first pillar properly. Let’s learn how to protect an object’s data and control how it changes.