Hard👤 8-15 years 2 min read

Breaking Parent Delegation — Interview Questions

Asked inOracleAmazonRed HatGoogle
#classloader#parent delegation#thread context classloader#spi#jdbc
Report issue

⚡ Short Answer

Normal delegation asks the parent first, so core classes always win. You break it in two classic cases: (1) SPI/JDBC, where a core class (DriverManager) must load an implementation from the application loader below it — solved with the Thread Context ClassLoader; and (2) servlet containers, which use 'child-first' (parent-last) loading so a web app's bundled library shadows the server's version. You break it by overriding loadClass to search locally before delegating.

Coffee Chat Question

Concept Made Simple

When and how do you deliberately break the parent-delegation model?

🧠Mind Map Answer

Remember It Faster

Default = ask your parent first (child-last). This guarantees you can't override java.lang.String. But sometimes a class high in the hierarchy needs to load something defined *below* it — delegation runs the wrong way for that.

SPI / JDBCCore code loads app impl via Thread Context CL
Servlet appsChild-first: WAR libs shadow server libs
HowOverride loadClass → try findClass before super
RiskShadowing core/JDK classes → subtle breakage

The Thread Context ClassLoader (Thread.currentThread().getContextClassLoader()) is the escape hatch: frameworks set it so parent-level code can reach child-level implementations without hard-coding a loader.

Key takeaway: break delegation only for SPI or intentional isolation — never to override JDK classes. Child-first is a scalpel, not a default.

⌨️Hands-on Keyboard

Learn by Doing

java
// SPI pattern: core code loads a provider it doesn't compile against
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class, tccl);
for (Driver d : drivers) {
    // 'd' was loaded by the application loader, not the bootstrap loader
    System.out.println("Loaded via: " + d.getClass().getClassLoader());
}

🔥What If?

Think Beyond the Expected

Why can't DriverManager (a core class) just load your JDBC driver directly?

Because the bootstrap/platform loader that defined DriverManager can't see the application classpath where your driver lives — delegation only goes upward. The Thread Context ClassLoader inverts that: the running thread carries a reference to the application loader, so core code can reach 'down' to load the concrete Driver. Without the TCCL, SPI (JDBC, JAXP, JNDI) couldn't work.

😂Real World

Every JDBC connection, every ServiceLoader-based plugin, and every servlet container relies on this. When you hit 'driver not found' or the wrong library version loading in an app server, it's almost always a delegation/TCCL issue — you fix it by setting or passing the right context loader.

🎯Interviewer's Expectation

Keywords they're listening for:

default = parent-first (child-last)SPI needs Thread Context ClassLoaderservlet = child-first/parent-lastoverride loadClass to invertnever shadow JDK classes

⚠️Common Mistakes

  • Using child-first loading to override core JDK classes
  • Forgetting to set the Thread Context ClassLoader in worker threads
  • Assuming delegation can search downward by default

Best Practices

  • Use the TCCL for SPI lookups, not a hard-coded loader
  • Restrict child-first shadowing to app libraries only
  • Document any custom loadClass override carefully

🔁Follow-up Questions

  • 1What is ServiceLoader and how does it use the TCCL?
  • 2What breaks if a web app bundles a JDK class?
  • 3How does JPMS (modules) change the loading picture?

🧩Related Technologies

ServiceLoaderJDBCServlet containersJPMS

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: "When and how do you deliberately break the parent-delegation model?"

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