Scrivio Interview library

Interfaces and abstract classes

18 questions in 6 subtopics.

Abstract classes vs interfaces

ConceptFoundations6 min read
Compare abstract classes and interfaces in modern Java (Java 8 and later). When would you choose an abstract class?
DesignIntermediate7 min read
The JDK often pairs an interface with an abstract skeletal implementation, such as List with AbstractList. Why, and when would you use the same pattern in your own library?
LeadershipAdvanced7 min read
An internal SDK used by 60 services exposes abstract classes for clients to extend. You want to move to interfaces. What compatibility and migration concerns do you plan for?

Default, static and private interface methods

CodingFoundations6 min read
Write an interface with a default method, a static factory method and a private helper, and explain the rules for each.
ConceptIntermediate6 min read
Why were default methods added in Java 8, and what risks do they bring when a library adds one to a widely implemented interface?
LeadershipPrincipal7 min read
Default methods were added to let the JDK evolve interfaces, but teams now use them to put large amounts of behaviour into interfaces. What org-wide guidance would you set, and what long-term costs are you guarding against?

Functional interfaces

ConceptFoundations6 min read
What makes an interface functional? Is Comparator a functional interface even though it declares equals? What does @FunctionalInterface do?
DebuggingIntermediate6 min read
A lambda passed to Stream.map needs to call a method that throws IOException. Engineers wrap it with try { ... } catch (IOException e) { throw new RuntimeException(e); } everywhere. What options do you weigh?
DesignIntermediate6 min read
When should a team define its own functional interface instead of using java.util.function types? Give an example where a custom one is clearly better.
ConceptPrincipal7 min read
Java added lambdas in Java 8 by targeting existing single-method interfaces rather than adding function types to the language. Explain why, what it costs (for example many specialised interfaces in java.util.function), and how that should shape your own API design.

Marker interfaces

ConceptFoundations6 min read
What is a marker interface? Give JDK examples and explain how Serializable and Cloneable work.
DesignIntermediate7 min read
Marker interface or annotation: which would you use to mark classes that may be published as events in your system? Justify the choice.
ScenarioAdvanced7 min read
Your platform's serialization library uses a marker interface to allow classes to be deserialized from external input. A security review finds gadget-chain risks. What do you change?

Diamond problem with default methods

CodingFoundations5 min read
Two interfaces A and B both declare default String hello(). Class C implements both. What happens, and how do you call A's version?
ConceptIntermediate6 min read
Explain the three rules Java uses to resolve which method is inherited when classes and interfaces provide the same method, and why Java avoids the C++ diamond problem.

Interface evolution and binary compatibility

ConceptIntermediate6 min read
Which changes to a published interface are binary compatible and which are source compatible? Consider adding an abstract method, adding a default method, and adding a static method.
DesignAdvanced7 min read
You maintain a widely used internal interface PaymentGateway. You must add a capability for refunds. Compare a new default method, a new interface, and a new major version.
LeadershipPrincipal6 min read
The JDK used default methods to evolve Collection without breaking a vast ecosystem. What lessons does that offer for governing APIs inside a company with hundreds of services and libraries?