ClassLoader Hierarchy and Parent Delegation
Fast recall
Quick revision
Core answer
Whether you understand how class identity works in Java — two classes loaded by different ClassLoaders are different types even if identical.
Thinking ClassCastException between same-named classes is a bug in the code, not a ClassLoader isolation issue.
Discuss OSGi, Java EE, and application servers (Tomcat) which break parent delegation for isolation, and how context ClassLoaders work.
Interview-ready guide
Interview answer
The ClassLoader hierarchy exists primarily as a security mechanism — parent-delegation ensures nobody can inject a fake java.lang.String onto the classpath.
Parent-delegation model:
- Bootstrap → Platform → Application — each loader asks its parent first before loading
- Bootstrap handles
java.baseclasses; Application handles your classpath - Ensures core JDK classes are always loaded by the trusted Bootstrap loader — no shadowing
Class identity gotcha:
- A class is identified by (fully qualified name + the ClassLoader that defined it)
- Same bytecode loaded through two different ClassLoaders → two unrelated types →
ClassCastExceptioneven though class names match - Comes up constantly in application servers, OSGi, and hot-reload scenarios
Child-first ClassLoaders (e.g. Tomcat):
- Deliberately breaks parent delegation — searches
WEB-INF/libbefore the parent - Allows different webapps to use different versions of the same library
- Risk: if anything in the parent scope holds a reference to a webapp class, the entire ClassLoader stays in memory → Metaspace leak on redeploy
I recommend understanding the context ClassLoader — SPI mechanisms like JDBC and ServiceLoader depend on it to find implementations loaded by child ClassLoaders.
ClassLoader Hierarchy and Parent Delegation
Direct Answer
Java uses parent-delegation: bootstrap → platform → application class loaders. A loader asks its parent first, then loads locally — prevents core classes from being replaced. Context class loaders break the model for frameworks (Spring, JPA).
Common Mistakes & Pitfalls
Thinking ClassCastException between same-named classes is a bug in the code, not a ClassLoader isolation issue.
Check your understanding
Answer these without looking back at the article.
- How does Tomcat isolate web applications using ClassLoaders?
- What is the context ClassLoader and when do you need it?
- How does OSGi use ClassLoaders for bundle isolation?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.