1. Home
  2. Interview Questions
  3. Java
  4. 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

0 0
InterviewQuestions Team ✔ Expert answer ✔ Best 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.lang and java.util from the java.base module. Because it is not a Java object, getClassLoader() returns null for 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 the jre/lib/ext directory.
  • 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:

  1. Check whether this loader has already loaded the class (findLoadedClass).
  2. If not, delegate the request to the parent loader.
  3. Only if the parent throws ClassNotFoundException, call its own findClass() to locate the bytes and define the class.

Why it exists:

  • Security: If you put your own java.lang.String on 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 with java. and throws a SecurityException.
  • 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 of findClass() 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 ClassCastException errors 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 ClassNotFoundException and NoClassDefFoundError?
  • 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 DriverManager or ServiceLoader need 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

All medium questions →

Related Questions

‹
Q18 of 18 · Java
All ›

Interview Questions

Free interview questions and answers, MCQ practice, online tests, mock interviews and group discussions — everything you need to crack your next interview.
📍 Dasardot LLC
1309 Coffeen Avenue STE 1200
Sheridan, Wyoming 82801
USA