OOPs Interview Questions in Java

Naveen AnamaneniUpdated · 6 min read

A school runs on roles: the principal, teachers, students and the office staff each know their own job and talk to each other through clear channels. Nobody needs to know how the office keeps its files to ask for a bonafide certificate. Object-oriented programming organizes code the same way, and because Java is built around it, OOP questions come up in every Java interview, from freshers to senior roles. This page collects the most common ones, from the four pillars to design principles. Answer each aloud first, then reveal the answer.

The Four Pillars

What are the four pillars of OOP?

Encapsulation (keep data private behind methods), inheritance (a class reuses and extends another), polymorphism (one interface, many implementations, chosen at runtime) and abstraction (show what an object does, hide how). Give a one-line example of each if asked.

What is encapsulation, and why is it useful?

Making fields private and exposing behavior through methods. The class controls its own state: it can validate changes (a balance can't go negative), change its internal design without breaking callers, and stay consistent.

What is the difference between abstraction and encapsulation?

Abstraction is about the outside view: which operations an object offers (an interface such as List). Encapsulation is about protecting the inside: keeping the data and implementation private. Abstraction decides what to show; encapsulation hides the rest.

What types of inheritance does Java support?

Single, multilevel and hierarchical inheritance for classes. Java doesn't allow a class to extend more than one class (no multiple inheritance of state), but a class can implement many interfaces.

Why doesn't Java support multiple inheritance of classes?

To avoid the diamond problem: if two parent classes had the same method or field, it would be unclear which one the child inherits. Interfaces with default methods can still clash, but then the compiler forces the class to override the method and choose.

Polymorphism

What is the difference between overloading and overriding?

Overloading: same method name, different parameter lists, in the same class; chosen at compile time. Overriding: a subclass provides its own version of an inherited method with the same signature; chosen at runtime by the object's actual type.

What are the rules for overriding a method?

Same name and parameters; the return type must be the same or a subtype (covariant); access can't be more restrictive; it can't throw broader checked exceptions; final, static and private methods can't be overridden. Always add @Override.

What is dynamic method dispatch?

The JVM chooses which overridden method to run based on the object's real class at runtime, not the variable's declared type. Shape s = new Circle(); s.area(); runs Circle.area().

Can a constructor be overridden or inherited?

No. Constructors aren't inherited, so they can't be overridden. A subclass constructor calls a parent constructor with super(...), explicitly or implicitly.

Design Questions

What is the difference between composition and inheritance?

Inheritance is an is-a relationship (Car extends Vehicle); composition is a has-a relationship (Car has an Engine field). Prefer composition: it is more flexible, can be changed at runtime, and doesn't expose the parent's internals.

What are association, aggregation and composition?

Association: any relationship between two classes. Aggregation: a has-a relationship where the part can exist alone (a department has teachers). Composition: a has-a relationship where the part's life depends on the whole (a house has rooms).

What are the SOLID principles?

Single responsibility (one reason to change), Open/closed (open for extension, closed for modification), Liskov substitution (subtypes must work wherever the parent is expected), Interface segregation (small, focused interfaces), Dependency inversion (depend on abstractions, not concrete classes).

What is the difference between an abstract class and an interface in design terms?

Use an interface to describe a capability that unrelated classes can share (Comparable, Runnable). Use an abstract class when related classes share state or code. They are often combined: List (interface) and AbstractList (skeletal implementation).

What is a sealed class, and how does it relate to OOP?

A sealed class or interface lists exactly which classes may extend it (permits). It lets you model a closed set of types, such as payment kinds, and with pattern matching for switch the compiler checks that every type is handled.

Polymorphism in Action

Let's see overloading, overriding and dynamic dispatch in one program.

Shapes.java▶ Try it
import java.util.List;
 
public class Shapes {
    static abstract class Shape {
        abstract double area();
 
        String describe() {
            return getClass().getSimpleName() + " with area "
                    + String.format("%.1f", area());
        }
    }
 
    static class Circle extends Shape {
        final double r;
        Circle(double r) { this.r = r; }
        @Override double area() { return Math.PI * r * r; }
    }
 
    static class Square extends Shape {
        final double side;
        Square(double side) { this.side = side; }
        @Override double area() { return side * side; }
    }
 
    static String print(Shape s) { return "shape: " + s.describe(); }
    static String print(Circle c) { return "circle: " + c.describe(); }
 
    public static void main(String[] args) {
        List<Shape> shapes = List.of(new Circle(1), new Square(2));
        for (Shape s : shapes) {
            System.out.println(s.describe());       // overriding, runtime
        }
        Shape s = new Circle(2);
        System.out.println(print(s));               // overloading, compile time
        System.out.println(print(new Circle(2)));
    }
}
Output
Circle with area 3.1
Square with area 4.0
shape: Circle with area 12.6
circle: Circle with area 12.6

Explanation: describe() calls area(), and the JVM runs the version of the actual object (circle or square): that is overriding with dynamic dispatch. But print(s) picks print(Shape), because overloading is decided at compile time from the declared type Shape, even though the object is a Circle.

Interview Corner

How do you give a real-world example of polymorphism?

Pick something with one action and many forms: "A Payment interface has pay(amount). UpiPayment, CardPayment and CashPayment each implement it differently, and the checkout code just calls payment.pay(total)."

What should you avoid when explaining OOP?

Reciting textbook definitions without examples, and examples that are too abstract (animals and sounds). Use something from real software: payments, shapes on a screen, notifications, file formats.

Practice

  1. Easy: Give a one-sentence real-world example for each of the four pillars, using a school as the setting.
  2. Harder: Add a Triangle to Shapes.java, then add static String print(Square sq) and predict what print(shapes.get(1)) prints before running it.

Quick check: OOP

4 questions
1Which is decided at compile time?
2Which relationship is "has-a" where the part can't exist without the whole?
3What does the L in SOLID stand for?
4Why does Java forbid extending two classes?

Key Takeaways

Was this tutorial helpful?

Lesson 570 of 578