The dynamic binding mechanism enables references to base classes to point to the correct subclass objects, thus enabling base class-oriented programming.
However, dynamic binding will fail in the following two situations.
1. The base class method is private or final modified
This is easy to understand, because private means that the method is invisible to the subclass. Write a method with the same name in the subclass is not to override the parent class method, but to regenerate a new method, so there is no problem of polymorphism. The same can also be explained by final, because the method is also unsurpassable.
2. The method is statically modified
The code is as follows.
class Base { public static void staticMethod() { System.out.println("Base staticMehtod"); } public void dynamicMehtod() { System.out.println("Base dynamicMehtod"); }}class Sub extends Base { public static void staticMethod() { System.out.println("Sub staticMehtod"); } public void dynamicMehtod() { System.out.println("Sub dynamicMehtod"); }}public class TJ4 { public static void main(String args[]) { Base c = new Sub(); c.staticMethod(); c.dynamicMehtod(); }}/* OutPut: Base staticMehtod Sub dynamicMehtod */The output result does not output "Sub staticMehtod" as expected. Because static methods are associated with a class rather than an object, c.staticMethod(); is equivalent to Car.staticMethod(); so try not to use instance variables to call static methods to avoid confusion.
The above detailed explanation of the Java static method is not polymorphic. This is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.