Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Object-Oriented Programming

    Interview Questions

    Real interview questions on Object-Oriented Programming, answered the way you would say them out loud.

    Extra: Interview prep5 min read

    Interview questions

    Explain the four pillars of OOP with examples.

    How I'd answer: "Encapsulation is hiding an object's state behind methods that enforce its rules — for example an Account with a private credits field that can only change through spend, which rejects negative balances. Inheritance lets a class reuse and extend another — MultipleChoice extends Question gets its text, points and grading logic. Polymorphism means one call behaves differently depending on the actual object — a loop calling question.grade(answer) runs the right isCorrect for each question type through dynamic dispatch, so adding a new type doesn't change the loop. Abstraction means exposing what something does and hiding how — Question is abstract, and code depends only on 'you can grade me'; interfaces take that further as pure contracts."

    Combines: Ch 1, 2, 4, 5, 6.

    Abstract class vs interface — when would you use each?

    How I'd answer: "A class can extend one abstract class but implement many interfaces. Abstract classes can have state, constructors and any kind of method, so they suit a family of closely related classes that share code and data. Interfaces define a capability or contract with no instance state — since Java 8 they can have default and static methods — and suit roles unrelated classes can share, like Comparable. My default is to define the type other code depends on as an interface, and add an abstract base class underneath only if several implementations really share state and logic. That keeps code depending on abstractions, and makes testing with fakes easy."

    Combines: Ch 5, 6.

    Why doesn't Java support multiple inheritance?

    How I'd answer: "To avoid the diamond problem: if a class inherited from two classes that both define the same method or have fields, it's ambiguous which implementation and which copy of the state it gets, and in languages that allow it you get complex resolution rules. Java allows only one superclass but many interfaces. Interfaces have no instance state, and when two default methods clash, the compiler forces the class to override the method and explicitly choose, like Gradable.super.label(). So you get multiple inheritance of type and behaviour, but not of state."

    Combines: Ch 2, 6.

    What's the equals and hashCode contract, and what happens if you break it?

    How I'd answer: "If two objects are equal according to equals, they must return the same hashCode. The reverse isn't required — collisions are allowed. equals must also be reflexive, symmetric, transitive, consistent and return false for null. Hash-based collections use the hash to pick a bucket and then equals within it. If I override equals but not hashCode, two equal objects usually get different identity hash codes, so HashSet.contains looks in the wrong bucket and returns false even though equals would say true — I've reproduced exactly that. So I always generate both from the same fields, prefer immutable keys, or just use a record."

    Combines: Ch 8, 12.

    Composition vs inheritance — which do you prefer and why?

    How I'd answer: "I favour composition. Inheritance couples the subclass to the parent's implementation — the fragile base class problem — and since Java has single inheritance, combining independent features forces a class explosion: timed quizzes, proctored quizzes, timed-and-proctored… With composition, the quiz holds a Timer and a Proctor behind interfaces and delegates to them, so any combination is just different parts passed in, parts can change at runtime, and each is testable alone. I use inheritance when there's a genuine, stable is-a relationship and the subclass can substitute for the parent everywhere — like question types sharing an abstract Question."

    Combines: Ch 2, 6, 7.