Scrivio Interview library

Object-oriented principles in Java

31 questions in 11 subtopics.

Encapsulation

ConceptFoundations6 min read
What is encapsulation in Java beyond making fields private and adding getters and setters? Give an example where getters and setters still break it.
ScenarioIntermediate7 min read
JPA entities in your service have public setters on every field, and business rules are scattered in service classes. What would you change, and what constraints do JPA and frameworks impose?

Inheritance and method overriding

ConceptFoundations7 min read
List the rules a method must follow to override another in Java, including access, return type, exceptions and static or private methods.
CodingFoundations6 min read
What does this print?
class Animal {
String name = "animal";
String speak() {
return "...";
}
}
class Dog extends Animal {
String name = "dog";
@Override
String speak() {
return "woof";
}
}
Animal a = new Dog();
System.out.println(a.name + " " + a.speak());
DesignAdvanced6 min read
Your shared framework exposes abstract base classes that 40 internal teams extend. Every change to a base class breaks someone. How do you redesign the extension model?

Polymorphism and dynamic dispatch

CodingFoundations7 min read
Replace this type-check chain with polymorphism and explain the benefit:
double area(Shape s) {
if (s instanceof Circle c) return Math.PI * c.r() * c.r();
else if (s instanceof Square q) return q.side() * q.side();
throw new IllegalArgumentException();
}
InternalsIntermediate8 min read
How does the JVM implement virtual method calls, and how does the JIT make them cheap in common cases?
ConceptPrincipal7 min read
Modern Java supports both object-oriented polymorphism and data-oriented pattern matching over sealed types. Explain the expression problem and how you would guide teams to choose between the two styles for long-lived domain code.

Method overloading resolution

CodingFoundations7 min read
Which method is called in each case?
void f(long x) {...}
void f(Integer x) {...}
void f(Object x) {...}
void f(int... x) {...}
f(5);
f(Integer.valueOf(5));
short s = 5;
f(s);
ConceptIntermediate5 min read
Overloading is resolved at compile time and overriding at run time. Show how this causes a surprising result with equals(Order other) declared in a class.
DebuggingIntermediate5 min read
A List<Integer> remove call deletes the wrong element: list.remove(index) where index is an int, versus list.remove(value) where value is an int. Explain and fix, then say how API design could avoid this.

Abstraction

ConceptFoundations6 min read
Explain abstraction with an example from the JDK, and how it differs from encapsulation.
LeadershipAdvanced7 min read
Teams in your organization create an interface for every class (OrderService and OrderServiceImpl) by default. Is this good abstraction? What guidance would you give?

Composition vs inheritance

DesignIntermediate7 min read
Why does Effective Java advise favouring composition over inheritance? Explain with the InstrumentedHashSet example and show the composed version.
ScenarioAdvanced7 min read
A legacy codebase has a five-level class hierarchy for payment processors with overridden hooks at every level. A new payment type needs behaviour from two branches. How do you approach the redesign?
LeadershipPrincipal7 min read
Java frameworks of the 2000s relied heavily on inheritance (base classes for servlets, DAOs, tests), while modern Java favours composition, interfaces and functions. How would you steer a large organization's codebases through this shift over several years?

SOLID principles

ConceptFoundations7 min read
Explain each SOLID principle in one or two sentences with a Java example.
ScenarioIntermediate7 min read
A code reviewer rejects a pull request for violating the open/closed principle because it adds a case to a switch. When is that criticism right and when is it wrong?
LeadershipPrincipal7 min read
SOLID was formulated in the 1990s and 2000s for object-oriented codebases. With records, sealed types, lambdas and microservices in modern Java, which parts still matter, which need reinterpretation, and how do you teach them to an organization?
LeadershipPrincipal7 min read
Your organization has over-applied design principles: many single-implementation interfaces, deep abstraction layers and factories everywhere, which slow delivery. How do you correct course without swinging to unstructured code?

Liskov substitution in practice

ConceptFoundations6 min read
Why is the classic Square extends Rectangle example a Liskov substitution violation? Show code that breaks.
DesignIntermediate6 min read
A ReadOnlyList class extends a mutable List implementation and throws UnsupportedOperationException from add. Is that a Liskov violation? How do the JDK's own collections handle this?
ScenarioAdvanced7 min read
A platform team replaces a cache client with a new implementation of the same interface. It returns empty results instead of throwing on timeout, and downstream services start serving stale prices. How do you treat this, and how do you prevent it?

Covariant return types

CodingFoundations6 min read
What is a covariant return type? Show an example with clone() and explain what the compiler generates.
DesignIntermediate6 min read
How do covariant return types help with fluent builder APIs in a class hierarchy, and what alternative do recursive generics offer?

Method hiding

CodingFoundations6 min read
What does this print, and why?
class Parent {
static String who() {
return "parent";
}
String name() {
return "P";
}
}
class Child extends Parent {
static String who() {
return "child";
}
String name() {
return "C";
}
}
Parent p = new Child();
System.out.println(p.who() + " " + p.name());
DebuggingIntermediate6 min read
A subclass declares a field protected String status; with the same name as the parent's field. Code that reads entity.status through a Parent-typed variable sees null, while methods in the subclass see the value. Explain and fix it.
LeadershipAdvanced7 min read
A legacy utility framework in your company relies on subclasses hiding static factory methods of base classes to change behaviour, and teams keep getting the wrong implementation. How would you guide the redesign?

Oop pillars overview

ConceptFoundations6 min read
Name the four pillars of object-oriented programming and give a one-line Java example of each.
DesignIntermediate6 min read
A candidate says that Java code using records, sealed interfaces and pattern matching is not object-oriented. How would you respond, and how do the four pillars apply to such a design?
LeadershipPrincipal6 min read
Interview loops in your organisation still ask juniors and seniors alike to list the four OOP pillars. How would you change what is assessed about object-oriented design at different levels?