Hard👤 8-15 years 2 min read

ClassLoader Architecture — Interview Questions

Asked inOracleAmazonMicrosoftRed Hat
#classloader#jvm#custom classloader#namespaces#class loading
Report issue

⚡ 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.

BootstrapNative; loads java.base core classes; parent = null
PlatformLoads platform JDK modules (was 'Extension')
ApplicationLoads your classpath/modulepath
Customextends 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.

⌨️Hands-on Keyboard

Learn by Doing

java
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:

Bootstrap/Platform/Application tiersloader defines a namespaceidentity = name + defining loaderfindClass + defineClassused 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

  • 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

TomcatOSGijava.lang.instrumentmodulepath

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

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.

Related Questions