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().
βCoffee Chat Question
Concept Made Simple
βExplain the ClassLoader hierarchy (Bootstrap, Platform, Application) and how you'd write a custom class loader.β
π§ Mind Map Answer
Remember It Faster
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.
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.
β¨οΈHands-on Keyboard
Learn by Doing
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
}
}π₯What If?
Think Beyond the Expected
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:
β οΈ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
- 1How does the parent-delegation model change loading order?
- 2When would you deliberately break delegation?
- 3How do class-loader leaks happen on redeploy?
π§©Related Technologies
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?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.