Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain the adapter pattern and give examples from the JDK and Spring.

The adapter pattern puts a small object between a client and a class whose interface does not fit, converting calls from the interface the client expects into calls the other class understands, without changing either side. In the JDK, Arrays.asList adapts an array to the List interface and InputStreamReader adapts a byte InputStream to a character Reader. In Spring MVC, HandlerAdapter lets DispatcherServlet call very different kinds of handlers through one interface. In Java, adapters are usually object adapters that wrap the adapted object by composition.

Why this question is asked

Every real system connects code that was not designed together: a library with its own types, a legacy module, a third-party API. The adapter pattern is the standard way to make them fit without rewriting either side, and once we know it we see it all over the JDK and Spring. Interviewers ask for examples because naming real ones shows we understand the idea rather than a textbook diagram. In this answer, we will recap the problem adapters solve, look at how an adapter is built, walk through Arrays.asList, InputStreamReader and Spring MVC's HandlerAdapter, and finish with object adapters compared with class adapters.

1A Quick Recap of the Problem

An interface, in the design sense, is the set of methods a piece of code offers or expects. A client is the code that calls it. The problem appears when a client expects one interface, say a List<String>, and what we have offers another, say a String[] array. Neither side is wrong, and often we cannot change either one: the client may be a library, and the other class may be third-party code.

An adapter is a new class that implements the interface the client expects and holds a reference to the object we have, called the adaptee. Each call from the client is translated into calls on the adaptee. The analogy is a travel plug adapter: the laptop plug and the wall socket stay as they are, and a small piece in the middle makes them fit.

2Arrays.asList: an Array Seen as a List

Arrays.asList(array) returns a List that is backed by the array. It does not copy the elements; it wraps the array and implements the List methods on top of it.

Java
String[] names = {"ann", "bob", "cid"};
List<String> list = Arrays.asList(names);
System.out.println(list.get(1)); // bob, read straight from the array
list.set(0, "amy");
System.out.println(names[0]); // amy: writes go through to the array
Collections.sort(list); // any List algorithm works on the array now
list.add("dan"); // throws UnsupportedOperationException

Notice that the adapter is honest about what the array can do. get() and set() map directly to array access, so they work and write through. add() would need a bigger array, which an array cannot be, so the adapter throws UnsupportedOperationException. This is typical of adapters: they translate what can be translated and refuse the rest. If we want an independent, growable list, we copy instead with new ArrayList<>(Arrays.asList(names)).

3InputStreamReader: Bytes Seen as Characters

An InputStream reads bytes. A Reader reads characters. Turning bytes into characters needs an encoding, such as UTF-8, because one character may be one byte or several. InputStreamReader takes an InputStream and a charset and presents itself as a Reader.

Java
InputStream bytes = new ByteArrayInputStream("h\u00e9".getBytes(StandardCharsets.UTF_8)); // 3 bytes
Reader chars = new InputStreamReader(bytes, StandardCharsets.UTF_8); // adapter
System.out.println((char) chars.read()); // h
System.out.println(chars.read()); // 233, the code point U+00E9, decoded from two bytes
System.out.println(chars.read()); // -1, end of stream

The client, here any code that wants a Reader, such as a BufferedReader or a CSV parser, never sees bytes. The adapter does the decoding. Always passing the charset matters: without it, older JDKs used the platform default encoding, and since JDK 18 the default is UTF-8, so being explicit makes the behaviour the same everywhere.

4Spring MVC's HandlerAdapter

DispatcherServlet is the front controller of Spring MVC: every request arrives there and is sent on to a handler. Handlers come in very different shapes. An annotated @Controller method has arbitrary parameters and return types. An old-style Controller interface has a handleRequest() method. An HttpRequestHandler has another signature again. DispatcherServlet does not want to know about each shape.

So Spring defines one interface, HandlerAdapter, with methods such as supports(handler) and handle(request, response, handler). For each handler type there is an adapter: RequestMappingHandlerAdapter for annotated methods, which resolves parameters and converts return values, SimpleControllerHandlerAdapter for the Controller interface, and HttpRequestHandlerAdapter for HttpRequestHandler. DispatcherServlet asks each adapter whether it supports the handler and then calls handle() on the one that does.

Step throughDispatcherServlet calling a handler through an adapter
DispatcherServletRequestMappingHandlerAdapter@Controller method
supports(handler)?
true
handle(request, response, handler)
orders(customerId, page) with resolved arguments
List<Order>
ModelAndView or a written response body

6 messages in order. Play them, or step through them one at a time.

DispatcherServlet talks to one interface; the adapter knows the handler's real shape.

Notice that adding a new kind of handler means adding a new adapter, without changing DispatcherServlet. That is the open-closed principle at work: open for extension, closed for modification.

5Object Adapters and Class Adapters

There are two ways to build an adapter. An object adapter holds the adaptee in a field and delegates to it; that is composition, and all three examples above work this way. A class adapter inherits from the adaptee and implements the target interface at the same time. Class adapters need multiple inheritance of classes, which Java does not have, so they only work when the target is an interface and the adaptee is a class we can extend, and they tie the adapter to one concrete class.

So in Java, adapters are almost always object adapters. They can wrap any implementation of the adaptee, including a subclass or a mock in a test, and they keep the adaptee's methods hidden, so the client only sees the target interface.

Adapter examples
AdapterClient expectsAdaptee offers
Arrays.asListList<T>an array T[]
InputStreamReaderReader of charactersInputStream of bytes
RequestMappingHandlerAdapterHandlerAdapter.handle()an annotated controller method
Your CarrierAdapteryour domain interfacea vendor SDK client

6Follow-up questions

How is an adapter different from a decorator?

An adapter changes the interface: the client sees a different type from the one being wrapped. A decorator keeps the same interface and adds behaviour, such as buffering or retry. BufferedReader wrapping a Reader is a decorator; InputStreamReader wrapping an InputStream is an adapter.

Is Collections.enumeration() an adapter?

Yes. It adapts a Collection to the old Enumeration interface for legacy APIs that still expect it, and Collections.list() goes the other way, turning an Enumeration into an ArrayList by copying.

When should an application write its own adapters?

Mostly at integration boundaries: wrapping a third-party SDK behind an interface in our own domain terms, so vendor types do not spread through the code and the vendor can be replaced or faked in tests.

7Conclusion

In this answer, we saw that an adapter converts the interface of one class into the interface a client expects, without changing either side. Arrays.asList adapts an array to a List and writes through to it, InputStreamReader adapts bytes to characters using a charset, and Spring MVC's HandlerAdapter lets DispatcherServlet call annotated methods and older handler types through one interface. In Java, adapters are object adapters built by composition, because Java has no multiple inheritance of classes.

What to say in the interviewAbout 80 seconds aloud

Open with

An adapter is an object that sits between a client and a class with the wrong interface. It implements the interface the client expects and translates each call into calls on the wrapped object, so neither side has to change.

Then, if they want more

A simple JDK example is Arrays.asList: it gives you a List view backed by an array, so get and set go straight to the array and even write through, while add throws UnsupportedOperationException because an array can't grow. Another is InputStreamReader, which adapts a byte InputStream into a character Reader by decoding with the charset you give it.

In Spring, the clearest one is HandlerAdapter in Spring MVC. DispatcherServlet has to call handlers of very different shapes: annotated controller methods, the old Controller interface, HttpRequestHandler. It only talks to the HandlerAdapter interface, asks each adapter whether it supports the handler, and calls handle on the right one; RequestMappingHandlerAdapter then resolves the method's arguments and converts its return value.

In Java, adapters are almost always object adapters using composition, because we don't have multiple inheritance of classes, and composition also lets the adapter wrap any implementation, including a mock.

About this answer

Last updated
12 November 2026
Versions it names
Java 18
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the adapter pattern and give examples from the JDK and Spring.": https://scrivio-interview-library.netlify.app/java/structural-patterns/3e27cfd365/