Java OOP Interview Questions and Answers: Inheritance, Polymorphism, Abstraction and More
Object-Oriented Programming questions are guaranteed in any Java interview β junior or senior. Interviewers use OOP questions to assess not just syntax knowledge but how you think about designing software.
This guide covers the most commonly asked questions with clear explanations and code examples.
The Four Pillars of OOP
Every Java OOP interview starts here. Know these cold.
1. Encapsulation
Encapsulation is bundling data (fields) and the methods that operate on that data into a single unit (class), and restricting direct access to internal state.
javapublic class BankAccount { private double balance; public double getBalance() { return balance; } public void deposit(double amount) { if (amount > 0) { balance += amount; } } public void withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; } } }
The balance field is private. Outside code cannot set it to a negative number directly β all access goes through controlled methods.
Why it matters: Encapsulation protects invariants and makes classes easier to change without breaking callers.
2. Inheritance
Inheritance lets a class (child/subclass) acquire properties and behaviors from another class (parent/superclass).
javapublic class Animal { protected String name; public Animal(String name) { this.name = name; } public void eat() { System.out.println(name + " is eating."); } } public class Dog extends Animal { public Dog(String name) { super(name); } public void bark() { System.out.println(name + " says: Woof!"); } }
Dog inherits eat() from Animal and adds bark().
Key rule: Java supports single inheritance for classes β a class can only extend one class. But a class can implement multiple interfaces.
3. Polymorphism
Polymorphism means "many forms." An object can be treated as its parent type, and the correct method is called based on the actual runtime type.
Compile-time polymorphism (method overloading):
javapublic class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }
Runtime polymorphism (method overriding):
javapublic class Animal { public String speak() { return "..."; } } public class Cat extends Animal { @Override public String speak() { return "Meow"; } } public class Dog extends Animal { @Override public String speak() { return "Woof"; } } // Polymorphism in action Animal[] animals = { new Cat(), new Dog() }; for (Animal a : animals) { System.out.println(a.speak()); // Meow, then Woof }
The method called depends on the actual object type, not the variable type.
4. Abstraction
Abstraction hides implementation details and exposes only what is necessary. In Java, achieved via abstract classes and interfaces.
javapublic abstract class Shape { public abstract double area(); public void printArea() { System.out.println("Area: " + area()); } } public class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double area() { return Math.PI * radius * radius; } }
Interface vs Abstract Class
This is one of the most asked Java interview questions.
| Interface | Abstract Class | |
|---|---|---|
| Methods | Abstract by default (Java 8+: default/static allowed) | Can have abstract and concrete |
| Fields | public static final only | Any type |
| Constructors | None | Can have |
| Inheritance | A class can implement many | A class can extend one |
| Use when | Defining a contract/capability | Sharing common implementation |
java// Interface β defines what a class CAN DO public interface Flyable { void fly(); default void land() { System.out.println("Landing..."); } } // Abstract class β defines what a class IS public abstract class Vehicle { protected int speed; public abstract void move(); public void stop() { speed = 0; } }
Interview tip: Prefer interfaces for defining types and contracts. Use abstract classes when you want to share implementation code between closely related classes.
Common OOP Interview Questions
Q: What is the difference between method overloading and overriding?
- Overloading β same method name, different parameters, resolved at compile time
- Overriding β subclass redefines parent method with the same signature, resolved at runtime
Q: What is the super keyword?
super refers to the parent class. Used to call the parent constructor (super(args)) or an overridden parent method (super.method()).
javapublic class Square extends Rectangle { public Square(int side) { super(side, side); // calls Rectangle constructor } }
Q: What is the difference between == and .equals()?
==compares references (memory address).equals()compares content (what the object represents)
javaString a = new String("hello"); String b = new String("hello"); System.out.println(a == b); // false β different objects System.out.println(a.equals(b)); // true β same content
Q: What does the final keyword do?
finalclass β cannot be extended (e.g.,String)finalmethod β cannot be overridden by subclassesfinalvariable β can only be assigned once
Q: What is the difference between an interface and a marker interface?
A regular interface has method declarations. A marker interface (like Serializable, Cloneable) has no methods β it simply marks a class as having a capability that the JVM or framework can check via instanceof.
Q: What is composition vs inheritance?
Inheritance is "is-a" (Dog IS-A Animal). Composition is "has-a" (Car HAS-A Engine).
java// Inheritance class Dog extends Animal { } // Composition class Car { private Engine engine; private GPS gps; public Car(Engine engine, GPS gps) { this.engine = engine; this.gps = gps; } }
Prefer composition over inheritance β it is more flexible and avoids tight coupling. A famous principle from the Gang of Four Design Patterns book.
Q: What are access modifiers in Java?
| Modifier | Same Class | Same Package | Subclass | Everywhere |
|---|---|---|---|---|
public | Yes | Yes | Yes | Yes |
protected | Yes | Yes | Yes | No |
| (default) | Yes | Yes | No | No |
private | Yes | No | No | No |
Q: How do you create an immutable class?
An immutable class's state cannot change after construction. String is the classic example.
Steps:
- Declare class as
final - Make all fields
private final - No setters
- Initialize all fields in the constructor
- Return defensive copies for mutable fields
javapublic final class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } }
SOLID Principles
S β Single Responsibility Principle
A class should have one reason to change.
java// Bad β handles both user logic AND email sending class UserService { public void createUser(User u) { ... } public void sendWelcomeEmail(User u) { ... } } // Good class UserService { public void createUser(User u) { ... } } class EmailService { public void sendWelcomeEmail(User u) { ... } }
O β Open/Closed Principle
Open for extension, closed for modification. Add new behavior by adding new code, not changing existing code.
L β Liskov Substitution Principle
Subtypes must be substitutable for their base types without altering correctness. If S extends T, you should be able to use S wherever T is expected.
I β Interface Segregation Principle
Prefer many small, specific interfaces over one large general-purpose one. Classes should not be forced to implement methods they do not use.
D β Dependency Inversion Principle
Depend on abstractions, not concretions. High-level modules should not depend on low-level modules directly.
java// Bad class OrderService { MySQLDatabase db = new MySQLDatabase(); // concrete dependency } // Good class OrderService { Database db; // depends on abstraction public OrderService(Database db) { this.db = db; } }
Practice Java OOP on Froquiz
OOP theory is important, but practice is what builds real confidence. Try our Java quiz on Froquiz β hundreds of questions across beginner, intermediate, and advanced levels covering OOP, collections, concurrency, and more.
Summary
- Encapsulation β protect internal state via access control
- Inheritance β reuse and extend behavior (single class inheritance in Java)
- Polymorphism β overloading at compile time, overriding at runtime
- Abstraction β hide complexity behind interfaces and abstract classes
- Interface vs abstract class β contract vs shared implementation
- Composition over inheritance β favor flexibility
- SOLID β the foundation of clean, maintainable OOP design
Master these concepts and you will be ready to tackle any Java OOP interview question β from junior roles to senior architect positions.