Java Interview Questions

Java Class Loading and the Parent Delegation Model

Explain the parent delegation model. Why does Tomcat break it for webapps?

Why Interviewers Ask This

Tests whether you really understand classloader isolation — core to plugin systems, app servers, and hot reload.

How to Answer

  1. 1

    Three built-in classloaders: Bootstrap (rt.jar, native C++), Extension/Platform (jre/lib/ext or modules), Application (classpath).

  2. 2

    Parent delegation: a classloader asks its parent first, only loads itself if parent fails. Rationale: core classes (java.lang.Object) always come from Bootstrap, can't be spoofed.

  3. 3

    Tomcat flips this for WEB-INF/classes — each webapp loads its own classes first, so different wars can use different library versions in the same JVM.

  4. 4

    SPI (ServiceLoader / JDBC driver) also breaks delegation via Thread.currentThread().getContextClassLoader(), so core code (in Bootstrap) can find implementations on the app classpath.

  5. 5

    Same .class loaded by different classloaders = two different Class objects, ClassCastException even though names match. Key insight for plugin isolation.

Sample Answer

Default is parent delegation — Bootstrap → Extension → Application, each delegates up before trying itself. This guarantees java.lang.* can't be replaced by a malicious jar. Two big deliberate violations: Tomcat's WebappClassLoader loads WEB-INF/classes first so different wars can ship different Jackson versions; and SPI via Thread.currentThread().getContextClassLoader() lets Bootstrap-loaded JDBC APIs find driver impls on the app classpath. Class identity = classloader + fully-qualified name. Same class, two loaders, two distinct Class objects — that's why hot reload frameworks throw ClassCastException when you hold a reference across a reload.

Pro Tips

  • Java 9+ replaced "Extension classloader" with "Platform classloader" for modules — the hierarchy logic is the same.

  • ClassNotFoundException vs NoClassDefFoundError: first is "never found"; second is "found once but linking failed or loader-visibility issue".

  • HireMe AI (即答侠) pairs this question with the Tomcat/SPI examples so interviewers see you've shipped plugin systems, not just read a book.

Frequently Asked Questions

Can I write my own classloader?

Yes — extend ClassLoader and override findClass. Plugin frameworks, hot reload (JRebel), and OSGi all do this.

Why does JDBC's DriverManager need the context classloader hack?

DriverManager is in rt.jar (Bootstrap), but driver impls are on the app classpath (Application). Bootstrap can't see Application, so it reaches "down" via the thread context classloader.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free