OOPs Concepts in Java: All 4 Pillars with Code Examples (2026)
Sep 05, 2026 9 Min Read 18815 Views
(Last Updated)
Ever wondered why Java is the backbone of so many enterprise applications, Android apps, and backend systems? A big part of the answer comes down to four foundational concepts: the four pillars of Object-Oriented Programming (OOPs).
If you’re preparing for a tech interview or just starting your Java journey, understanding these pillars isn’t optional. It’s the entry ticket.
In this article, you’ll learn exactly what OOPs is, what each of the four pillars means, how they work in real Java code, and why they matter in actual software development. No draggy stuff, just clear explanations, real examples, and code you can run yourself.
Table of contents
- TL/DR Summary
- What is OOPs (Object-Oriented Programming) in Java?
- The 2 Building Blocks: Object and Class
- The 4 OOPs Concepts in Java: Explained with Examples
- Encapsulation: Protecting Your Data
- Inheritance: Don't Repeat Yourself
- Polymorphism: One Interface, Many Forms
- Abstraction: Show What Matters, Hide What Doesn't
- OOPs Concepts in Java: Definition, Code Example, and Real-World Analogy
- Abstraction vs Encapsulation: The Confusion Cleared
- OOPs in Java vs Python — Key Syntax Differences
- OOPs Interview Questions in Java — Top 20 Asked at Product Companies
- What is OOPs in Java, and why do we use it over procedural programming?
- Can you explain the four pillars of OOPs with a real project example, not just theory?
- What's the difference between a class and an object?
- Why is Java not considered 100% object-oriented?
- What's the difference between method overloading and overriding?
- Can you override a static method in Java?
- What is constructor overloading, and why can't constructors be overridden?
- Why does Java not support multiple inheritance with classes?
- What's the difference between an abstract class and an interface?
- If interfaces can now have default methods, what's the real difference left between abstract classes and interfaces?
- What is encapsulation actually protecting against?
- Why do we make instance variables private and expose getters and setters?
- What is the "is-a" and "has-a" relationship in OOPs?
- Why is composition often preferred over inheritance in real-world design?
- What is runtime polymorphism, and how does the JVM implement it internally?
- Can a constructor be private in Java? What's the use case?
- What happens if a subclass doesn't implement all abstract methods of its parent?
- What is the difference between abstraction and encapsulation, in one line an interviewer would accept?
- Why does Java use super keyword, and where can it be used?
- How would you design a class hierarchy for a real system — say a food delivery app — using all four OOPs pillars?
- Conclusion
- FAQs
- What are the 4 pillars of OOPs in Java?
- What is the difference between abstraction and encapsulation in Java?
- What are the types of polymorphism in Java?
- What are the types of inheritance in Java?
- Is Java 100% object-oriented?
TL/DR Summary
Java’s OOPs is built on four pillars:
- Encapsulation hides your data and controls access.
- Inheritance lets classes reuse code from a parent class.
- Polymorphism allows one method to behave differently based on context.
- Abstraction hides complexity and exposes only what’s necessary.
What is OOPs (Object-Oriented Programming) in Java?
Object-Oriented Programming (OOPs) in Java is a programming paradigm that organizes code around objects and classes rather than functions and logic alone. Java is one of the most widely used OOPs languages in the world, and its entire ecosystem, from Spring Boot to Android, is built on OOPs principles.
OOPs makes your code more modular, reusable, secure, and easier to maintain. Instead of writing one giant block of instructions, you model your program around real-world entities (objects) that have properties (data) and behaviors (methods).
Java isn’t considered 100% object-oriented; it still uses primitive data types like int, char, and boolean that aren’t objects. That’s a classic interview trap!
If you want to take this further and build full Java-based applications from the ground up with mentor guidance, then check out HCL GUVI’s Certified Java Full Stack Developer Course. You’ll go from OOPs fundamentals all the way to deploying real-world projects and in the end you will be getting an industry-grade certification!
The 2 Building Blocks: Object and Class

Every OOPs program in Java revolves around two core building blocks (Class and Objects) before the four pillars even come into play.
A Class is a blueprint. It defines what properties and behaviors an object will have, but it doesn’t occupy memory on its own. Think of it like an architectural drawing for a house.
An Object is a real instance of that class, the actual house built from the blueprint. It lives in memory and has actual values.
// Class = Blueprint
class Car {
String brand;
int speed;
void accelerate() {
System.out.println(brand + " is accelerating at " + speed + " km/h");
}
}
// Object = Real Instance
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Creating an object
myCar.brand = "Toyota";
myCar.speed = 120;
myCar.accelerate(); // Output: Toyota is accelerating at 120 km/h
}
}
Simple enough, right? Now let’s get to the four pillars that make OOPs truly powerful.
If you are planning on learning OOPs through a structured self-paced course, then consider enrolling in HCL GUVI’s Certified Object-Oriented Programming (OOPs) Course, which is perfect for beginners looking to master OOPs fundamentals. It covers key concepts like classes, objects, inheritance, polymorphism, and design patterns.
The 4 OOPs Concepts in Java: Explained with Examples
These four concepts are the heart of every Java interview, every Java project, and every Java framework you’ll ever work with. Let’s break each one down properly.
1. Encapsulation: Protecting Your Data

Encapsulation in OOPs is all about keeping your data safe from outside interference. You wrap your data (fields) and the methods that operate on that data inside a class, then restrict direct access using access modifiers. External code can only interact with your data through controlled methods, called getters and setters.
Think of it like your bank account. You can check your balance and withdraw money through the ATM, but you can’t directly reach into the bank’s database and change your balance. The data is encapsulated.
In Java, encapsulation is achieved by:
- Declaring class fields as private
- Providing public getter and setter methods to read and update those fields
class BankAccount {
private double balance; // Hidden from outside world
// Controlled access through getter
public double getBalance() {
return balance;
}
// Controlled update through setter
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(5000);
System.out.println("Balance: " + account.getBalance()); // Balance: 5000.0
// account.balance = 99999; ← This would cause a compile error!
}
}
Why it matters: Encapsulation makes your code easier to maintain, debug, and test. You can change the internal implementation without breaking any code that uses the class.
These four pillars only really click once you write your own classes and see inheritance and polymorphism play out live. HCL GUVI’s online IDE lets you write, compile, and run Java code instantly, so you can experiment with the examples from this guide and build your own OOPs mini-projects.
2. Inheritance: Don’t Repeat Yourself

Inheritance allows one class (the child or subclass) to inherit the properties and methods of another class (the parent or superclass). Instead of rewriting the same code, you build on what already exists.
This is the “IS-A” relationship in Java. A Dog IS-A Animal. A Tesla IS-A Car. The child gets everything the parent has, plus it can add its own unique features.
// Parent class
class Animal {
String name;
void eat() {
System.out.println(name + " is eating.");
}
}
// Child class inheriting from Animal
class Dog extends Animal {
void bark() {
System.out.println(name + " says: Woof!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Bruno";
dog.eat(); // Inherited from Animal → Bruno is eating.
dog.bark(); // Dog's own method → Bruno says: Woof!
}
}
Types of Inheritance in Java:
| Type | Description | Supported in Java? |
| Single | One child inherits from one parent | Yes |
| Multilevel | Child → Parent → Grandparent chain | Yes |
| Hierarchical | Multiple children from one parent | Yes |
| Multiple | One child from multiple parents | Not via classes (use interfaces) |
| Hybrid | Combination of the above | Not directly (use interfaces) |
3. Polymorphism: One Interface, Many Forms

The word “polymorphism” literally means many forms. In Java, it means you can use a single method name to perform different tasks depending on the context. This makes your code flexible, clean, and scalable.
There are two types you need to know:
Compile-time Polymorphism (Method Overloading): The method name is the same, but the parameters differ. Java decides which version to call at compile time.
class Calculator {
// Same method name, different parameters
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(5, 3)); // 8
System.out.println(calc.add(5.5, 3.2)); // 8.7
System.out.println(calc.add(1, 2, 3)); // 6
}
}
Runtime Polymorphism (Method Overriding): The child class provides a new version of a method already defined in the parent class. Java decides which version to call at runtime.
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle");
}
}
class Rectangle extends Shape {
@Override
void draw() {
System.out.println("Drawing a rectangle");
}
}
public class Main {
public static void main(String[] args) {
Shape s1 = new Circle();
Shape s2 = new Rectangle();
s1.draw(); // Drawing a circle
s2.draw(); // Drawing a rectangle
}
}
Overloading vs Overriding: Quick Comparison:
| Feature | Method Overloading | Method Overriding |
| Where | Same class | Parent & Child class |
| Parameters | Must differ | Must be same |
| Return type | Can differ | Must be same (or covariant) |
| Resolved at | Compile time | Runtime |
4. Abstraction: Show What Matters, Hide What Doesn’t

Abstraction is about simplifying complexity. You expose only the essential features of an object and hide the internal implementation details. Users of your class don’t need to know how something works, just that it works.
You use an ATM every day without knowing the internal banking logic, right? That’s abstraction in real life.
In Java, abstraction is achieved through two mechanisms:
- Abstract Classes: Can have both abstract (unimplemented) and concrete (implemented) methods
- Interfaces: Fully abstract by default (all methods are abstract unless marked default or static)
// Abstract class — defines the "what", not the "how"
abstract class Vehicle {
String brand;
// Abstract method — no implementation here
abstract void fuelType();
// Concrete method — shared implementation
void startEngine() {
System.out.println(brand + "'s engine started.");
}
}
class ElectricCar extends Vehicle {
@Override
void fuelType() {
System.out.println(brand + " runs on electricity.");
}
}
class PetrolCar extends Vehicle {
@Override
void fuelType() {
System.out.println(brand + " runs on petrol.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v1 = new ElectricCar();
v1.brand = "Tesla";
v1.startEngine(); // Tesla's engine started.
v1.fuelType(); // Tesla runs on electricity.
}
}
Abstract Class vs Interface: Know the Difference:
| Feature | Abstract Class | Interface |
| Methods | Abstract + Concrete | Abstract (+ default/static) |
| Variables | Any type | public static final only |
| Inheritance | extends (single) | implements (multiple) |
| Constructor | Can have | Cannot have |
| Use when | Shared base behavior | Define a contract/capability |
OOPs Concepts in Java: Definition, Code Example, and Real-World Analogy
Here’s the table below covering all four pillars of OOP, along with their definitions, Java examples, and real-world analogies:
| OOPs Concept | Definition | Java Example | Real-World Analogy |
|---|---|---|---|
| Encapsulation | Bundling data (variables) and methods that operate on that data into a single unit (class), while restricting direct access to some of it | private double balance; public double getBalance() { return balance; } | A medicine capsule — the ingredients are sealed inside, and you can only access them the way the capsule allows (swallowing it), not by breaking it open directly |
| Inheritance | A class (child) acquiring the properties and behaviors of another class (parent), so common code doesn’t have to be rewritten | class Car extends Vehicle { } | A child inheriting traits from a parent — you’re born with your parents’ eye color or height without having to “create” those traits yourself |
| Polymorphism | The same method behaving differently depending on the object calling it | Animal a = new Dog(); a.makeSound(); // Barks | A single button on a universal remote — pressing “power” turns on a TV, but the same button press turns on an AC when pointed at it instead |
| Abstraction | Hiding complex implementation details and showing only the essential features to the user | abstract class Shape { abstract void draw(); } | Driving a car — you use the steering wheel and pedals without needing to know how the engine or transmission works internally |
Abstraction vs Encapsulation: The Confusion Cleared
This is one of the most commonly asked interview questions, and honestly, it trips up a lot of people. Here’s the clearest way to think about it:
| Abstraction | Encapsulation | |
| Focus | Hiding complexity | Hiding data |
| Goal | Show only what’s relevant | Protect internal state |
| Achieved via | Abstract classes, Interfaces | Private fields, Getters/Setters |
| Real-world | ATM interface hides banking logic | ATM PIN hidden from the screen |
| Level | Design level | Implementation level |
One-liner to remember: Abstraction is about what an object does. Encapsulation is about how it protects what it has.
OOPs in Java vs Python — Key Syntax Differences
The core ideas of OOPs stay the same across languages, but Java and Python express them quite differently — one leans on strict rules and explicit syntax, the other keeps things loose and readable. Here’s a side-by-side look:
| Aspect | Java | Python |
|---|---|---|
| Class Declaration | class Car { } — requires a file name to match the public class name | class Car: — no matching filename requirement, indentation defines the body |
| Constructor | Uses a method named exactly after the class: public Car() { } | Uses a fixed special method: def __init__(self): |
| Access Modifiers (Encapsulation) | Enforced by the compiler using private, protected, public keywords | Convention-based only — a single underscore _var signals “protected,” double underscore __var triggers name-mangling; nothing is truly enforced |
| Inheritance Syntax | class ElectricCar extends Car { } | class ElectricCar(Car): |
| Method Overriding (Polymorphism) | Requires matching method signature; @Override annotation is optional but recommended for clarity | No annotation needed — just redefine the method with the same name in the child class |
| Abstraction | Needs an explicit abstract class or interface keyword, with abstract methods left unimplemented | Uses the abc module — inherit from ABC and mark methods with @abstractmethod |
OOPs Interview Questions in Java — Top 20 Asked at Product Companies
Before going through the questions below: product-based companies rarely stick to a fixed question bank — interviewers frame their own variations to test real understanding, not memorized answers.
1. What is OOPs in Java, and why do we use it over procedural programming?
It’s a programming approach built around objects instead of functions and logic. Interviewers want you to say it makes code reusable, scalable, and easier to maintain — not just recite the four pillars.
2. Can you explain the four pillars of OOPs with a real project example, not just theory?
This is where bookish answers fail. They want you to map encapsulation, inheritance, polymorphism, and abstraction to something you’ve actually built — like a payment module or a user authentication system.
3. What’s the difference between a class and an object?
Class is the blueprint, object is the actual instance created from it — but be ready for the follow-up: “how many objects can one class create?”
4. Why is Java not considered 100% object-oriented?
Because of primitive data types like int, char, boolean — they aren’t objects, unlike everything in Python.
5. What’s the difference between method overloading and overriding?
Overloading happens at compile-time within the same class with different parameters; overriding happens at runtime between parent and child classes with the same signature.
6. Can you override a static method in Java?
No — static methods belong to the class, not the object, so they get hidden, not overridden. This trips up a lot of candidates.
7. What is constructor overloading, and why can’t constructors be overridden?
Constructors can have multiple versions with different parameters (overloading), but they can’t be overridden since they aren’t inherited.
8. Why does Java not support multiple inheritance with classes?
To avoid the diamond problem — ambiguity when two parent classes have the same method. Java gets around this using interfaces instead.
9. What’s the difference between an abstract class and an interface?
Abstract classes can have both implemented and unimplemented methods; interfaces (pre-Java 8) could only have method declarations. Post-Java 8, interfaces can have default and static methods too — expect this nuance to be tested.
10. If interfaces can now have default methods, what’s the real difference left between abstract classes and interfaces?
Abstract classes support constructors and instance variables with state; interfaces still can’t maintain object state the same way.
11. What is encapsulation actually protecting against?
Uncontrolled or accidental modification of an object’s internal state from outside code — not just “hiding data” as a vague concept.
12. Why do we make instance variables private and expose getters and setters?
To control how the variable is read or updated — you can add validation logic inside the setter instead of allowing direct, unchecked access.
13. What is the “is-a” and “has-a” relationship in OOPs?
“Is-a” represents inheritance (a Car is a Vehicle); “has-a” represents composition (a Car has an Engine). Interviewers often ask when to prefer one over the other.
14. Why is composition often preferred over inheritance in real-world design?
Because inheritance creates tight coupling between classes — changes in the parent class can break child classes unexpectedly. Composition keeps things more flexible.
15. What is runtime polymorphism, and how does the JVM implement it internally?
Also called dynamic method dispatch — the JVM decides which overridden method to call based on the actual object type at runtime, not the reference type.
16. Can a constructor be private in Java? What’s the use case?
Yes — commonly used in Singleton design pattern implementations, where you want to control and restrict object creation.
17. What happens if a subclass doesn’t implement all abstract methods of its parent?
The subclass itself must be declared abstract too, otherwise it won’t compile.
18. What is the difference between abstraction and encapsulation, in one line an interviewer would accept?
Abstraction hides complexity in design (what to show), encapsulation hides data in implementation (how to protect it).
19. Why does Java use super keyword, and where can it be used?
To access parent class methods, constructors, or variables when a child class overrides or hides them.
20. How would you design a class hierarchy for a real system — say a food delivery app — using all four OOPs pillars?
This is a design-thinking question. They’re checking if you can apply OOPs concepts to a live problem, not just define them — expect to sketch classes like Order, Restaurant, DeliveryPartner, and justify your inheritance/composition choices out loud.
Conclusion
In conclusion, OOPs isn’t just a theoretical concept you mug up for interviews and forget. It’s the practical architecture of every Java application built at scale. You now understand what Encapsulation, Inheritance, Polymorphism, and Abstraction really mean: not just in definition, but in actual working code.
The best way to truly lock these in? Build something. Take any small real-world scenario, a library system, a vehicle management app, or a simple banking program, and implement it using all four OOPs pillars. You’ll be surprised how quickly the concepts click.
FAQs
1. What are the 4 pillars of OOPs in Java?
The four pillars are Encapsulation (data hiding through access modifiers), Inheritance (reusing code via parent-child class relationships), Polymorphism (one method, multiple behaviors through overloading and overriding), and Abstraction (hiding complexity through abstract classes and interfaces).
2. What is the difference between abstraction and encapsulation in Java?
Abstraction hides complexity at the design level using abstract classes and interfaces. Encapsulation hides data at the implementation level using private fields and getter/setter methods. Abstraction is about what an object does; encapsulation is about how it protects its data.
3. What are the types of polymorphism in Java?
Java supports two types: compile-time polymorphism (method overloading, same method name, different parameters) and runtime polymorphism (method overriding, child class redefines a parent class method).
4. What are the types of inheritance in Java?
Java supports single, multilevel, and hierarchical inheritance through classes. Multiple and hybrid inheritance are achieved through interfaces, since Java doesn’t allow a class to extend multiple classes.
5. Is Java 100% object-oriented?
No. Java uses primitive data types (int, char, boolean, float, etc.) that are not objects. This makes Java not purely object-oriented. However, Java provides wrapper classes (like Integer, Character) to treat primitives as objects when needed.



Did you enjoy this article?