[Java-based] - java parents delegate mechanism

Before looking at the parents delegate mechanism, you should know classloader (if not understand, you can now go to study intensively Kazakhstan)

Four kinds classloader

image.png

  1. Virtual machine comes
  • Bootstrap class loader (Bootstrap ClassLoader)
  • Extension class loader (Extension ClassLoader)
  • System class loader (Application ClassLoader)
  1. Custom
  • User-defined class loader needs to continuejava.lang.ClassLoader
package java.lang;

public abstract class ClassLoader {
    ...
}

Parents delegate mechanism

When a class loader loads a class, their first not load, but delegated to the parent class loader, if the parent class loader up as well as the parent class loader then in turn commissioned up until the topmost loader. If the loader can load the class, then loaded loaded by the class, if not by the child class loader to load. If all the class loader can not load exception will be thrown ClassNotFoundException

The advantages of delegating parents mechanism

  1. Loading a class with priority relationship, this relationship can be avoided by repeated load
  2. Avoid contamination of the source code
    such as: a user-defined class java.lang.String
package java.lang;
class String {
    public static void main(String[] args) {
        System.out.println("........")
    }
}

Users expect to be able to load custom java.lang.String, but in fact the result of:

错误: 在类 java.lang.String 中找不到 main 方法, 请将 main 方法定义为:
   public static void main(String[] args)
否则 JavaFX 应用程序类必须扩展javafx.application.Application

The main method is not found? ? ? So there is no proof jvm load custom String, but to find a java core of java.lang.String.
This prevents contamination of the core code of java

Guess you like

Origin www.cnblogs.com/amberbar/p/11711024.html