Today comes a very interesting question: what happens in Java if a subinterface defines a method that already exists in the parent interface? Like what:
Interface Irunnable extends runnable{void run ();
At first I thought that the syntax of this kind of grammar should not be passed through the compiler, and did not expect this to be the compiler did not make any warning.
Of course we wouldn't do it in most cases, because it doesn't seem to make much sense. But for the truth, I made a small realization:
public class Interfacedebug{public static void Main (string[] args) {irunnable irun1=new irunnableimpl (); Irun1.run (); Runnable Irun=irun1;irun.run (); irun=new IRunnableImpl2 (); Irun.run ();}} Interface Irunnable extends runnable{void run (); Class Irunnableimpl implements Irunnable{public void Run () {System.out.println ("irunnable of Irunnableimpl");}} Class IRUNNABLEIMPL2 implements Runnable{public void Run () {System.out.println ("irunnable of IRunnableImpl2");}}
The result of the operation is:
Irunnable of Irunnableimplirunnable of irunnableimplirunnable of IRUNNABLEIMPL2
As you can see, the Run method defined in Irunnable is actually run in runnable, and they both refer to the same thing and do not overwrite the method.
The same method as the parent interface is defined in the Java sub-interface