How does the Java ClassLoader hierarchy work and how does Parent Delegation prevent security breaches?
In Simple Words: Java loads classes dynamically on-demand using a tree of ClassLoaders. Under the Parent Delegation Model, when a ClassLoader is requested to load a class, it always delegates the request to its parent ClassLoader first before searching its own classpath.
Hierarchy:
Bootstrap ClassLoader (Native C++ core JVM classes: java.lang.*)
▲
Platform / Extension ClassLoader (Java modules / extensions)
▲
Application / System ClassLoader (Application classpath / JARs)
▲
Custom ClassLoaders (Plugin loaders, Web server classloaders)
Security Protection:
Prevents malicious code from injecting a rogue version of critical classes like java.lang.String or java.lang.System. Because delegation reaches the Bootstrap ClassLoader first, the official JVM core class is always loaded, rejecting fraudulent user overrides.