OOP in Java
Object-oriented programming (OOP) organises a program around objects — bundles of data and the behaviour that acts on that data — rather than around loose functions and variables. Java is an OOP-first language: almost everything lives inside a class. OOP rests on four pillars: encapsulation, inheritance, polymorphism, and abstraction. Together they help you model real-world things, reuse code, and keep large programs manageable.
Classes and objects
A class is a blueprint; an object is a concrete instance built from that blueprint. A Student class describes what every student has and can do; each enrolled person is an object of that class.
// Blueprint: what every Student has and does.
class Student {
String name; // a field (state)
int marks;
void study() { // a method (behaviour)
System.out.println(name + " is studying.");
}
}
We will go deeper into classes and objects in their own tutorial — here, focus on the four ideas that make code object-oriented.
Pillar 1 — Encapsulation
Encapsulation means hiding an object's internal data behind a controlled interface. You mark fields private and expose public methods (getters/setters) that guard how the data changes.
class BankAccount {
private double balance; // hidden — no outside code can set it directly
public void deposit(double amount) {
if (amount > 0) { // the method enforces a rule
balance += amount;
}
}
public double getBalance() { // read-only access
return balance;
}
}
Because balance is private, no one can secretly set it to a negative value — the deposit method protects the invariant.
Pillar 2 — Inheritance
Inheritance lets one class reuse and extend another. A subclass automatically gets the parent's fields and methods and can add its own.
class Person {
String name;
void greet() { System.out.println("Hi, I am " + name); }
}
// Student "is a" Person — it inherits name and greet().
class Student extends Person {
String course;
}
Student did not redeclare name or greet(); it inherited them. This avoids duplication.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesPillar 3 — Polymorphism
Polymorphism means "many forms": the same method call behaves differently depending on the actual object. A subclass can override a parent method.
class Shape {
double area() { return 0; }
}
class Circle extends Shape {
double radius;
@Override
double area() { return Math.PI * radius * radius; } // its own version
}
Code that holds a Shape reference can call area() and get the right result whether the object is really a Circle or another shape — the JVM picks the correct method at runtime.
Pillar 4 — Abstraction
Abstraction means exposing what something does while hiding how. You define an abstract type (an abstract class or interface) that declares behaviour without full implementation, leaving details to concrete classes.
abstract class Payment {
abstract void pay(double amount); // no body — every subclass must define it
}
class UpiPayment extends Payment {
@Override
void pay(double amount) {
System.out.println("Paid " + amount + " via UPI");
}
}
Callers work with the Payment type and do not care whether it is UPI, card, or cash — that is abstraction.
Why OOP matters in Java
These pillars are not academic. Encapsulation keeps bugs local, inheritance and abstraction cut duplication, and polymorphism lets you add new types without rewriting old code. The entire Java standard library is built this way, so understanding OOP is essential to reading and writing real Java.
Common mistakes
- Making every field public. That breaks encapsulation; default to
privateand expose only what callers need. - Overusing inheritance. "Is-a" relationships warrant inheritance; for "has-a", prefer composition (holding an object as a field).
- Forgetting
@Override. It is optional but catches typos — if your method does not actually override anything, the compiler flags it. - Confusing class and object. The class is the blueprint; the object is the thing created with
new. One class, many objects. - Putting all logic in one giant class. OOP encourages small, focused classes that each model one idea.
FAQ
Is Java purely object-oriented? Almost — but primitives (int, double, etc.) are not objects, which is a pragmatic exception for performance.
Do I need all four pillars in every program? No. They are tools; use the ones a given design calls for.
Keep going
Go deeper with Classes & Objects in Java, then Inheritance and Polymorphism. All tutorials are on the Java hub; for guided practice in Jalgaon, see the Java course.
Master object-oriented thinking at Infoplanet, Jalgaon. Join the waitlist for the Java course.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in Java
Store many values of one type with Java arrays: declaring, initializing, indexing, iterating, multidimensional arrays, and the Arrays helper class.
Classes & Objects in Java
Define classes with fields and methods, create objects with new, use the this keyword, and understand instance vs static members in Java.
The Java Collections Framework
Use the Java Collections Framework — List, Set, and Map with ArrayList and HashMap — plus generics and iteration, to store flexible groups of objects.
