What is Object-Oriented Programming (OOP) in Java?
Learn what object-oriented programming means in Java, covering encapsulation, inheritance, polymorphism, and abstraction with clear code examples.
Expected Interview Answer
Object-oriented programming is a paradigm that models software as objects bundling state (fields) and behavior (methods) together, and Java implements it through four pillars: encapsulation, inheritance, polymorphism, and abstraction.
Encapsulation hides an object's internal data behind private fields and exposes controlled access via public methods. Inheritance lets a class reuse and extend behavior from a parent class using extends. Polymorphism allows the same method call to behave differently depending on the actual runtime type, most commonly through method overriding. Abstraction lets you define what an object does through interfaces or abstract classes without fixing exactly how, which keeps code loosely coupled and easier to extend.
- Encapsulation protects internal state from invalid external changes
- Inheritance promotes code reuse across related classes
- Polymorphism enables flexible, extensible behavior at runtime
- Abstraction decouples what code does from how it does it
- Overall it produces more maintainable, modular codebases
AI Mentor Explanation
Encapsulation is like a captain keeping the exact fielding plan private in the huddle, revealing to the batsman only the public result: where the fielders stand. Inheritance is a young all-rounder inheriting core batting technique from a mentor before adding personal flair.
Step-by-Step Explanation
Step 1
Encapsulation
Keep fields private and expose behavior through public getters/setters or methods to protect invariants.
Step 2
Inheritance
Use extends to let a subclass reuse and specialize a superclass's fields and methods.
Step 3
Polymorphism
Override methods so calling the same method name on different subtypes produces type-specific behavior at runtime.
Step 4
Abstraction
Define contracts via interfaces or abstract classes so callers depend on behavior, not concrete implementation.
Step 5
Compose objects into a system
Combine these pillars so classes collaborate through well-defined, decoupled interfaces rather than tangled logic.
What Interviewer Expects
- Names all four pillars correctly
- Can give a concrete Java example of inheritance and polymorphism
- Understands why encapsulation matters (invariant protection)
- Distinguishes abstract classes from interfaces at a high level
- Explains OOP's benefit in terms of maintainability, not just definitions
Common Mistakes
- Listing only two or three of the four pillars
- Confusing overloading with polymorphism (overriding)
- Saying encapsulation just means 'using private fields' without mentioning controlled access
- Treating inheritance as always the right tool instead of composition
- Describing abstraction and encapsulation as the same concept
Best Answer (HR Friendly)
“Object-oriented programming organizes code around real-world-like objects that bundle data and behavior together. It relies on four ideas: hiding internal details, reusing code between related objects, letting the same action behave differently depending on the object, and defining what something does without locking in exactly how — all of which makes large codebases easier to build and maintain.”
Code Example
abstract class Shape {
abstract double area(); // abstraction
}
class Circle extends Shape { // inheritance
private final double radius; // encapsulation
Circle(double radius) { this.radius = radius; }
@Override double area() { return Math.PI * radius * radius; } // polymorphism
}
class Square extends Shape {
private final double side;
Square(double side) { this.side = side; }
@Override double area() { return side * side; }
}
public class Main {
public static void main(String[] args) {
Shape[] shapes = { new Circle(2), new Square(3) };
for (Shape s : shapes) {
System.out.println(s.area()); // 12.566..., 9.0 - same call, different behavior
}
}
}Follow-up Questions
- What is the difference between an abstract class and an interface?
- How does method overloading differ from method overriding?
- Why might you favor composition over inheritance?
- What is the difference between compile-time and runtime polymorphism?
- How does encapsulation help enforce class invariants?
MCQ Practice
1. Which are the four pillars of OOP?
OOP in Java is built on encapsulation, inheritance, polymorphism, and abstraction.
2. What does polymorphism most commonly rely on in Java?
Runtime polymorphism is achieved through overriding, where a subclass provides its own implementation of a superclass method.
3. What does encapsulation primarily protect?
Encapsulation hides internal fields behind controlled access points to preserve valid object state.
Flash Cards
Name the four pillars of OOP. — Encapsulation, inheritance, polymorphism, and abstraction.
What is polymorphism in Java? — The same method call producing different behavior depending on the object's actual runtime type.
How does encapsulation work in Java? — Fields are made private and accessed only through public methods that enforce validation.
What keyword implements inheritance in Java? — extends for classes, implements for interfaces.