Interfaces and abstract classes
18 questions in 6 subtopics.
Abstract classes vs interfaces
Compare abstract classes and interfaces in modern Java (Java 8 and later). When would you choose an abstract class?The JDK often pairs an interface with an abstract skeletal implementation, such as List withAbstractList. Why, and when would you use the same pattern in your own library?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
Write an interface with a default method, a static factory method and a private helper, and explain the rules for each.Why were default methods added in Java 8, and what risks do they bring when a library adds one to a widely implemented interface?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
What makes an interface functional? Is Comparator a functional interface even though it declares equals? What does@FunctionalInterface do?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?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.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
What is a marker interface? Give JDK examples and explain how Serializable and Cloneable work.Marker interface or annotation: which would you use to mark classes that may be published as events in your system? Justify the choice.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
Two interfaces A and B both declaredefault String hello(). Class C implements both. What happens, and how do you call A's version?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
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.You maintain a widely used internal interfacePaymentGateway. You must add a capability for refunds. Compare a new default method, a new interface, and a new major version.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?No question matches these choices.