The reason why dynamic polymorphism of static methods in Java is not supported

Why does Java not support dynamic polymorphism of static methods?

Conversely, what happens if Java supports runtime polymorphism for static methods?

Solution

Your assumption is wrong It never calls an instance of a class It's always called class

Try the following sample code and you will never get a NullPointerException

class ABC {
    public static void hello() {
        System.out.println("Hello");
    }
}

ABC abc = null;
abc.hello();

Polymorphism occurs when overriding a method in a subclass Because static methods belong to classes, there is no meaning to override static methods Therefore, polymorphism always applies to methods that belong only to class instances

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>