Java / JVM Architecture (Java21) Interview questions
Why do we use the parent delegation model in class loading?
Parent delegation exists mainly for security and consistency, not performance.
Security-wise, it stops untrusted or application-supplied code from shadowing core classes - if you defined your own java.lang.String, delegation ensures the Bootstrap ClassLoader's trusted version is still the one found first, since the request travels up before any lower loader gets to try.
Consistency-wise, it guarantees a given class is loaded once by a single loader and shared, rather than every loader in the chain independently reloading and creating multiple, incompatible Class objects for what should be the same type - which would otherwise break basic assumptions like instanceof and casting across the application.
More Related questions...