ClassLoader Architecture — Interview Questions
Reviewed by Gurusankar M.
Short Answer
Java uses a hierarchy of loaders: Bootstrap (loads java.base / core JDK, written in native code, parent = null), Platform (formerly Extension, loads JDK modules), and Application/System (loads your classpath). Each loader defines a namespace, so a class's identity is (fully-qualified name + defining loader). You write a custom loader by extending ClassLoader and overriding findClass() to fetch bytes and call defineClass().
On this page
Concept Made Simple
Coffee Chat Question
“Explain the ClassLoader hierarchy (Bootstrap, Platform, Application) and how you'd write a custom class loader.”
Remember It Faster
Mind Map Answer
A class's runtime identity is name + the loader that defined it — the same com.app.Foo loaded by two different loaders are two *distinct* classes that can't be cast to each other. That fact is the root of most 'class loading' interview questions.
- Bootstrap
- Native; loads java.base core classes; parent = null
- Platform
- Loads platform JDK modules (was 'Extension')
- Application
- Loads your classpath/modulepath
- Custom
- extends ClassLoader, override findClass → defineClass
Custom loaders power plugins, hot reload, and isolation: app servers give each web app its own loader so two apps can use different versions of the same library without clashing.
Key takeaway: loaders create isolated namespaces. If you ever see ClassCastException: Foo cannot be cast to Foo, two loaders defined the same class — that's the tell.
Learn by Doing
Hands-on Keyboard
class InMemoryClassLoader extends ClassLoader {
private final Map<String, byte[]> classBytes;
InMemoryClassLoader(ClassLoader parent, Map<String, byte[]> bytes) {
super(parent); // keep delegation intact
this.classBytes = bytes;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] b = classBytes.get(name);
if (b == null) throw new ClassNotFoundException(name);
return defineClass(name, b, 0, b.length); // bytes -> Class
}
}Think Beyond the Expected
What If?
Why does the same class loaded by two loaders cause a ClassCastException?
Because class identity in the JVM is (fully-qualified name, defining loader) — not the name alone. Two loaders each running defineClass on the same bytes create two separate Class objects with separate static state. Assigning one to a reference typed by the other fails, even though the source is identical. This is exactly why plugin/app-server isolation works — and why it bites you when types leak across loader boundaries.
Real World
Tomcat/WildFly give every deployed WAR its own loader so app A can use Jackson 2.12 and app B Jackson 2.16 simultaneously; OSGi and IDE plugin systems do the same. Build tools and bytecode agents use custom loaders to instrument or hot-swap classes.
Interviewer's Expectation
Keywords they're listening for:
- Bootstrap/Platform/Application tiers
- loader defines a namespace
- identity = name + defining loader
- findClass + defineClass
- used for isolation/plugins
Common Mistakes
- Thinking a class name alone identifies a class
- Overriding loadClass instead of findClass (breaks delegation)
- Forgetting each loader has its own static state
Best Practices
- Override findClass, not loadClass, to preserve delegation
- Give plugins/apps isolated loaders for version independence
- Null out custom loaders on undeploy to avoid leaks
Follow-up Questions
- How does the parent-delegation model change loading order?
- When would you deliberately break delegation?
- How do class-loader leaks happen on redeploy?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Class Loading (Advanced Java) Interview question: "Explain the ClassLoader hierarchy (Bootstrap, Platform, Application) and how you'd write a custom class loader." Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair