What are the OOP Concepts in Java?
Master the four OOP concepts in Java — encapsulation, inheritance, polymorphism, and abstraction — with clear definitions and code for your interview.
Expected Interview Answer
Java's object-oriented programming rests on four core concepts: encapsulation (bundling data with the methods that operate on it and hiding internals), inheritance (deriving new classes from existing ones), polymorphism (one interface behaving differently across types), and abstraction (exposing essential behaviour while hiding implementation).
Objects are instances of classes that combine state (fields) and behaviour (methods). Encapsulation uses access modifiers like private with getters and setters to protect data. Inheritance with extends promotes code reuse and models is-a relationships. Polymorphism appears as compile-time method overloading and runtime method overriding through dynamic dispatch. Abstraction is achieved with abstract classes and interfaces, letting callers depend on what an object does rather than how it does it.
- Encapsulation protects data and reduces coupling
- Inheritance enables code reuse and clear hierarchies
- Polymorphism makes code flexible and extensible
- Abstraction hides complexity behind simple interfaces
- Together they improve maintainability and scalability
- Models real-world entities intuitively
AI Mentor Explanation
OOP maps onto a cricket team neatly. Encapsulation is a player keeping their private training routine hidden while exposing only their on-field performance. Inheritance is an all-rounder who inherits both batting and bowling skills from base roles. Polymorphism is the call 'take the field' meaning bat, bowl, or keep depending on the player. Abstraction is the captain trusting a bowler to get wickets without knowing their exact grip.
Step-by-Step Explanation
Step 1
Model with classes and objects
Define classes as blueprints combining fields (state) and methods (behaviour), then create objects from them.
Step 2
Apply encapsulation
Make fields private and expose controlled access through getters, setters, and public methods.
Step 3
Use inheritance
Derive subclasses with extends to reuse and specialise behaviour, modelling is-a relationships.
Step 4
Enable polymorphism
Override methods for runtime dispatch and overload methods for compile-time flexibility.
Step 5
Design with abstraction
Use abstract classes and interfaces so callers depend on behaviour, not implementation details.
What Interviewer Expects
- Naming all four pillars: encapsulation, inheritance, polymorphism, abstraction
- A clear definition of each with a Java example
- Distinguishing overloading (compile-time) from overriding (runtime)
- Knowing abstraction uses abstract classes and interfaces
- Understanding access modifiers support encapsulation
Common Mistakes
- Confusing abstraction with encapsulation
- Mixing up method overloading and overriding
- Thinking Java supports multiple inheritance of classes
- Forgetting that interfaces enable abstraction and polymorphism
- Making all fields public and calling it encapsulation
Best Answer (HR Friendly)
“Object-oriented programming in Java is built on four ideas: hiding an object's inner details, letting one class reuse another's features, allowing the same action to behave differently for different objects, and exposing only what matters while hiding the complexity. Together they make code easier to reuse and maintain.”
Code Example
// Abstraction: an interface defines what a shape does, not how
interface Shape {
double area();
}
// Encapsulation: fields are private, accessed via a method
abstract class BaseShape implements Shape {
private String name;
BaseShape(String name) { this.name = name; }
public String getName() { return name; }
}
// Inheritance: Circle reuses BaseShape
class Circle extends BaseShape {
private final double radius;
Circle(double radius) { super("Circle"); this.radius = radius; }
// Polymorphism (overriding): area() behaves per type
@Override public double area() { return Math.PI * radius * radius; }
}
class Rectangle extends BaseShape {
private final double w, h;
Rectangle(double w, double h) { super("Rectangle"); this.w = w; this.h = h; }
@Override public double area() { return w * h; }
}
public class OopDemo {
public static void main(String[] args) {
Shape[] shapes = { new Circle(2), new Rectangle(3, 4) };
for (Shape s : shapes) {
// Runtime polymorphism picks the right area()
System.out.printf("%.2f%n", s.area());
}
}
}Follow-up Questions
- What is the difference between method overloading and overriding?
- How does Java achieve multiple inheritance of type using interfaces?
- What is the difference between an abstract class and an interface?
- How do access modifiers support encapsulation?
- What is runtime polymorphism and how does dynamic dispatch work?
MCQ Practice
1. Which OOP concept hides internal state and exposes controlled access?
Encapsulation bundles data with methods and restricts direct access using private fields and public accessors.
2. Method overriding is an example of which concept?
Overriding is resolved at runtime through dynamic dispatch, making it runtime (dynamic) polymorphism.
3. How does Java support multiple inheritance of type?
A class can extend only one class but implement many interfaces, giving multiple inheritance of type.
Flash Cards
What are the four OOP pillars? — Encapsulation, inheritance, polymorphism, and abstraction.
What is encapsulation? — Bundling data with methods and hiding internal state behind access modifiers.
Overloading vs overriding? — Overloading is compile-time (same name, different parameters); overriding is runtime (subclass redefines a method).
Abstract class vs interface? — An abstract class can have state and partial implementation; an interface primarily declares behaviour a class implements.