The output is:
This is B
This is C
In the above example, class B and class C are two classes that implement the interface InterA. They implement the method fun() of the interface respectively. By assigning instances of class B and class C to the interface reference a, the dynamics of the method at runtime are realized. Binding makes full use of "one interface, multiple methods" to demonstrate Java's dynamic polymorphism.
One thing to note is that when Java uses interface variables to call a method of an object of its implementation class, the method must have been declared in the interface, and the type and parameters of the implementation method in the implementation class of the interface must be the same as those in the interface. Defined exact match.
Conclusion: The above is the implementation method of Java runtime polymorphism. You can use it flexibly during the programming process. However, the use of runtime polymorphism is not recommended in codes with high performance requirements. After all, Java's runtime dynamic method calls are compared with The system overhead of ordinary method calls is relatively large.