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.
On this page
Concept Made Simple
Coffee Chat Question
“When and how do you deliberately break the parent-delegation model?”
Remember It Faster
Mind Map Answer
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 / JDBC
- Core code loads app impl via Thread Context CL
- Servlet apps
- Child-first: WAR libs shadow server libs
- How
- Override loadClass → try findClass before super
- Risk
- Shadowing 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.
Learn by Doing
Hands-on Keyboard
// 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());
}Think Beyond the Expected
What If?
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 ClassLoader
- servlet = child-first/parent-last
- override loadClass to invert
- never 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
- What is ServiceLoader and how does it use the TCCL?
- What breaks if a web app bundles a JDK class?
- How does JPMS (modules) change the loading picture?
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?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair