Quick revision
The short answer
ClassLoader.loadClass first checks whether this loader has already loaded the class, then asks its parent, and only if the parent cannot find it does it call its own findClass. Because every request climbs to the bootstrap loader first, java.lang.String always resolves to the JDK's copy, and the JVM also refuses outright to define any class in a java.* package from an ordinary loader, and modules allow java.lang to live only in java.base.
What to remember
In this answer, we saw that loadClass checks its own cache, delegates to its parent, and calls findClass only when every ancestor has failed. That order means java.lang.String always comes from the bootstrap loader, while the prohibited package check stops anyone from defining java.* classes directly and the module system lets java.lang live only in java.base. Delegation exists to give every class one shared view of core types and to avoid duplicate definitions, which is why custom loaders override findClass and leave loadClass alone.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ClassLoader.loadClass first checks whether this loader has already loaded the class, then asks its parent, and only if the parent cannot find it does it call its own findClass. Because every request climbs to the bootstrap loader first, java.lang.String always resolves to the JDK's copy, and the JVM also refuses outright to define any class in a java.* package from an ordinary loader, and modules allow java.lang to live only in java.base.
Why this question is asked
Every class our code touches reaches the JVM through a class loader, and most of the time we never think about which one. The question comes up in interviews because the delegation rule explains a whole family of real problems: why a library on the class path cannot replace a JDK class, why a plugin sees the host's copy of an interface, and why writing a custom loader the wrong way breaks everything. In this answer, we will recap what a class loader is, walk through loadClass step by step, see exactly what stops a home-made java.lang.String, and finish with why delegation is designed this way and how a custom loader should fit into it.
1A Quick Recap of Class Loaders
A class loader is an object that turns a class name such as com.acme.Order into a loaded class the JVM can run. It finds the bytes of the .class file somewhere (a directory, a jar, the network, or memory), hands them to the JVM to be checked and defined, and remembers the result.
Loaders form a tree. At the top is the bootstrap loader, built into the JVM, which loads the core of the JDK such as java.lang.Object and java.lang.String from the java.base module. Below it is the platform loader, which loads the remaining JDK modules such as java.sql. Below that is the application loader (also called the system loader), which loads our classes from the class path or module path. Every loader except bootstrap has a parent, and custom loaders we create normally have the application loader as their parent.
One more term matters here: the JVM identifies a class not by its name alone but by its name together with the loader that defined it. Two classes called com.acme.Order defined by two different loaders are two different types, and an object of one cannot be cast to the other.
- Bootstrap loaderbuilt into the JVM;
java.basesuch asjava.lang.String - Platform loaderother JDK modules such as
java.sqlandjava.xml - Application loaderour class path and module path
- Custom loadersplugins, scripts, web applications; parent is usually the application loader
4 layers, from the top down. Step through them one at a time.
A request starts at the bottom and climbs to the top before any loader searches its own files.
2What loadClass Does, Step by Step
The logic lives in java.lang.ClassLoader.loadClass(String name, boolean resolve). Stripped to its essentials, it looks like the code below.
- Already loaded by this loader?YesReturn the cached classNoAsk the parent
- Parent (and its parents) found it?YesReturn the parent's classNoCall this loader's findClass
- findClass found the bytes?YesdefineClass and return itNoThrow ClassNotFoundException
3 steps in order. Play them, or step through them one at a time.
Parent first: a loader searches its own files only after all its ancestors have failed.
Notice the order. First, findLoadedClass checks the loader's own cache, so a class is defined at most once per loader. Second, the request goes to the parent, which runs the very same method, so the request climbs all the way to bootstrap before anyone searches. Third, only when every ancestor has said no does this loader call findClass, the method that actually locates bytes and calls defineClass.
3Why Our Own java.lang.String Is Never Used
Suppose we write our own java/lang/String.java, compile it, and put it on the class path. When any code asks the application loader for java.lang.String, the loader checks its cache, then delegates to the platform loader, which delegates to bootstrap. Bootstrap finds String in java.base immediately and returns it. Our copy is never even read, because the application loader never gets to step three.
What if we bypass loadClass and try to define it directly, for example from a custom loader that calls defineClass with our bytes? The JVM has a second guard for exactly this case. ClassLoader refuses to define any class whose name starts with java. unless the caller is the JDK's own platform or bootstrap code.
So there are two independent barriers. Delegation means the real String is found first, and the prohibited package check means a fake one cannot be defined even if we try. This check works without any Security Manager; it is built into ClassLoader itself.
4The Module System Adds a Third Barrier
Since Java 9, packages belong to modules, and the java.lang package belongs to the java.base module. Within one module layer, a package may belong to only one module. The boot layer already has java.lang in java.base, so no other module can contain a java.lang package; the module system rejects the configuration before our code runs. Classes on the class path live in the unnamed module, and if they declare a package that a named module already owns, the class path copies are simply never found for that package.
5Why Delegation Is Designed This Way
The point of parent-first delegation is a consistent view of shared types. Every class in the JVM, whichever loader defined it, sees the same java.lang.String, the same java.util.List and the same java.lang.Object. If each loader could define its own String, a String created in a plugin could not be passed to a method in the host that expects java.lang.String, because the two would be different types. Core code would break, and security checks written in JDK classes could be replaced by anyone.
Delegation also avoids duplicate definitions. A class that sits in a parent is defined once and shared by every child, which saves memory and keeps static state single. A plugin interface placed in the application loader is the same type for the host and for every plugin, which is what lets the host cast plugin objects to it.
6Writing a Custom Loader That Respects Delegation
This leads straight to the follow-up question. When we write our own loader, we override findClass, not loadClass. loadClass already contains the cache check, the delegation and the locking. findClass is the hook the JDK designers left for us: it is called only after the parent has failed, and its only job is to find bytes and define them.
If we overrode loadClass instead and searched our directory first, we would be writing a child-first loader. That is sometimes done on purpose, as servlet containers do, but it must still send java.* and the shared API to the parent, or the plugin and the host end up with incompatible copies of the same interface.
7Follow-up questions
Why should a custom loader override findClass rather than loadClass?
Because loadClass holds the delegation logic, the cache check and the per-name locking, and overriding it means re-implementing all of that correctly. findClass is called only after the parent failed, so by overriding it we add a new place to look for classes while keeping the guarantee that JDK and shared classes always come from the parent. Override loadClass only when you deliberately want a different search order, and then still delegate java.* and shared API packages first.
Can two loaders both load com.acme.Order?
Yes, if each one finds the class itself instead of delegating, for example two sibling plugin loaders that each contain the class. The JVM then has two distinct types with the same name. An object of one cast to the other fails with a ClassCastException whose message names both loaders, which is the classic symptom of a class present in two places.
8Conclusion
In this answer, we saw that loadClass checks its own cache, delegates to its parent, and calls findClass only when every ancestor has failed. That order means java.lang.String always comes from the bootstrap loader, while the prohibited package check stops anyone from defining java.* classes directly and the module system lets java.lang live only in java.base. Delegation exists to give every class one shared view of core types and to avoid duplicate definitions, which is why custom loaders override findClass and leave loadClass alone.
Open with
loadClass works parent first. It checks findLoadedClass to see whether this loader already defined the class, then delegates to the parent, and the parent does the same, so the request climbs to the bootstrap loader before anyone searches.
Then, if they want more
Only if all ancestors fail does the loader call its own findClass, which reads bytes and calls defineClass. So if I put my own java.lang.String on the class path, it is never used: the bootstrap loader finds the real String in java.base first, and my copy is never read.
If I try to bypass that by calling defineClass for java.lang.String from a custom loader, ClassLoader rejects it with a SecurityException, Prohibited package name: java.lang. That check is in ClassLoader itself, not the old Security Manager.
On top of that, with the module system a package can belong to only one module in a layer, and java.lang belongs to java.base, so no other module can add it.
The reason for all this is consistency: every class in the JVM must see the same core types, otherwise a String from one loader could not be passed to code expecting another, and shared classes would be defined twice. That is also why a custom loader overrides findClass and not loadClass, so it keeps delegation working.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain parent-first delegation in ClassLoader.loadClass. Why can an application not...": https://scrivio-interview-library.netlify.app/java/class-loading/d028588c68/