Breaking Parent Delegation β Interview Questions
Reviewed by Gurusankar M.
β‘ 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.
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
// 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:
β οΈ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
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.
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.