The dynamic binding mechanism makes it possible for base class programming to refer to the correct subclass object, so that a reference to the base class can be directed.
However, dynamic binding can fail in the following two situations.
1, the base class method is private or final modified
This is well understood, because private explains that the method is not visible to subclasses, and that a subclass that writes a method with the same name does not duplicate the parent-class method (Override) but regenerates a new method, and there is no polymorphic problem. The same can also explain final, because the method is also not covered.
2, the method is static modification
The code looks like this.
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 does not output "Sub staticmehtod" as envisaged. Because a static method is associated with a class rather than an object, C.staticmethod () is equivalent to Car.staticmethod (); So try not to use instance variables to invoke static methods to avoid confusion.
The above Java static method does not have the polymorphism of the details is small to share all the content of the people, I hope to give you a reference, but also hope that we support the cloud-dwelling community.