- Home
- Interview Questions
- Java
- How does the class loader delegation model work in Java, and…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: When a class loader is asked to load a class, it first asks its parent, and the request travels up to the bootstrap loader. A loader defines the class itself only if its parents cannot find it. This parent-first design stops application code from replacing core classes like java.lang.String, avoids loading the same class twice, and keeps class identity consistent.
Detailed explanation
The JVM does not load every class at startup. It loads classes lazily, when they are first needed, using a hierarchy of class loaders. In Java 9 and later, the built-in hierarchy is:
- Bootstrap class loader: part of the JVM itself. It loads core platform classes such as
java.langandjava.utilfrom thejava.basemodule. Because it is not a Java object,getClassLoader()returnsnullfor these classes. - Platform class loader: loads other Java SE and JDK modules, such as
java.sql. It replaced the older Extension class loader used in Java 8, which loaded JARs from thejre/lib/extdirectory. - Application (system) class loader: loads your own classes and library JARs from the classpath or module path.
The default ClassLoader.loadClass() logic works in three steps:
- Check whether this loader has already loaded the class (
findLoadedClass). - If not, delegate the request to the parent loader.
- Only if the parent throws
ClassNotFoundException, call its ownfindClass()to locate the bytes and define the class.
Why it exists:
- Security: If you put your own
java.lang.Stringon the classpath, it is never used, because the bootstrap loader always finds the real one first. The JVM also rejects user-defined classes in packages starting withjava.and throws aSecurityException. - No duplicates: A class already loaded by a parent is shared by all of that parent's children.
- Consistent identity: At runtime, a class is identified by its fully qualified name plus the loader that defined it. Two classes with the same name but different defining loaders are different types. Casting an object of one to the other throws
ClassCastException.
When delegation is deliberately changed: Application servers such as Tomcat give each web application its own loader. That loader typically checks the application's own WEB-INF classes before asking its parent, although it still never overrides core java.* classes. This lets two apps use different versions of the same library. Frameworks like OSGi go further. Service-loading mechanisms such as JDBC drivers rely on the thread context class loader, so core code can load implementation classes that its own parent loader cannot see.
Example
public class LoaderDemo {
public static void main(String[] args) {
// Core class: loaded by bootstrap, prints null
System.out.println(String.class.getClassLoader());
// java.sql module: platform loader (Java 9+)
System.out.println(java.sql.Driver.class.getClassLoader());
// Your class: application loader
System.out.println(LoaderDemo.class.getClassLoader());
System.out.println(LoaderDemo.class.getClassLoader().getParent());
}
}
// Custom loader: override findClass, not loadClass,
// so parent-first delegation is preserved
class MyLoader extends ClassLoader {
MyLoader(ClassLoader parent) { super(parent); }
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] bytes = readBytesFromSomewhere(name); // e.g. file, network, encrypted JAR
if (bytes == null) throw new ClassNotFoundException(name);
return defineClass(name, bytes, 0, bytes.length);
}
private byte[] readBytesFromSomewhere(String name) { return null; }
}On Java 9 or later, the output shows null, then the PlatformClassLoader, then the AppClassLoader, and finally the PlatformClassLoader again as the AppClassLoader's parent.
Common mistakes
- Saying the hierarchy is Bootstrap, Extension, Application without mentioning that the Extension loader was replaced by the Platform loader in Java 9.
- Overriding
loadClass()instead offindClass()in a custom loader, which breaks delegation unless done on purpose. - Believing that a class is identified only by its name. This is why the same class loaded by two loaders causes confusing
ClassCastExceptionerrors such as "com.x.User cannot be cast to com.x.User". - Claiming that delegation is inheritance. The parent is a reference held by the loader, not a superclass relationship.
Follow-up questions
- What is the difference between
ClassNotFoundExceptionandNoClassDefFoundError? - Why do servlet containers use child-first loading for web applications, and what problems can it cause?
- What is the thread context class loader, and why does JDBC's
DriverManagerorServiceLoaderneed it?
Interview tip: Explain the three steps of loadClass() and the point that class identity is the name plus the loader. That one point is what separates experienced answers from memorised ones.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More medium Java questions
- What are the constraints in Java generics and why are they used? · 1 ans
- What are the different types of garbage collectors in Java? · 1 ans
- What are the advantages of using interfaces in Java? · 1 ans
- What are static methods and when should you use them? · 1 ans
- What happens when you cast a subclass object to a superclass type? · 1 ans
Related Questions
- What is the difference between JDK, JRE and JVM? · Java · 2 ans
- How does the JVM manage memory for Java applications? · Java · 1 ans
- What are the different types of garbage collectors in Java? · Java · 1 ans
- How is a lambda different from an anonymous inner class in Java beyond shorter syntax? · Java · 1 ans
- What makes an interface a functional interface, and what does @FunctionalInterface actually do? · Java · 1 ans
- How does HashMap work internally, including collisions, resizing and treeification in Java 8? · Java · 1 ans
- What is the difference between HashSet, LinkedHashSet and TreeSet, and when would you use each? · Java · 1 ans
- How do you implement a generic interface and its functions in Java? · Java · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)