Interface callbacks are another manifestation of polymorphism. An interface callback means that a reference to an object created by a class that uses an interface can be assigned to an interface variable declared by that interface, so that the interface variable can invoke methods in the interface that is implemented by the class. When an interface variable calls a method in an interface that is implemented by a class, it is the method that notifies the corresponding object to invoke the interface and becomes the interface callback. Different classes may have different functions when they use the same interface. That is, the method body of the interface does not have to be the same, so the interface callback may produce different behavior.
The interface callback facilitates the extension of the function, which is implemented by the framework layer to handle the custom function outside the main function.
Now there are two classes, one is a, one is B, the callback is a to call a method in B, and then B callback a method in a, this method is the callback method, the callback mechanism is inseparable from the interface.
That is, Class A implements an interface and implements the interface method, and Class B gets a reference to the callback function, and then calls the swap function. It seems to be more around, give a popular example.
1 //Callback Interface2 equivalent to the teacher's office address3 Public InterfaceCallBack {4 Public voidCallback (String homework);5 }6 //teacher appears as an upper -level application7 Public classTeacherImplementscallback{8 @Override9 Public voidCallback (String homework) {TenSystem.out.println ("Teacher, this is my homework ..." +homework); One } A } - - //students appear as lower -level identities the Public classStudent {//equivalent to the teacher's office - PrivateCallBack CallBack =NULL; - Public voidSetcallback (CallBack CallBack) { - This. CallBack =CallBack; + } - Public voiddohomework () { +System.out.println ("The Writing industry ... (half an hour later) the homework was finished.); A //tell the teacher that the homework has been finished and has been handed over to the office atCallback.callback ("A math paper")); - } - } - Public classTest { - Public Static voidMain (string[] args) { -Teacher T =NewTeacher (); inStudent s =NewStudent (); - //incoming callback object teacher to S.setcallback (t); + s.dohomework (); - } the}
The above is an example of a callback.
Callback is a callback interface, is the office address, the teacher is in the upper level, its students do not know and do not understand,
So the teacher wants to let the student hand over the work to the office, also must tell the student office the address, therefore the teacher this class has to implement this callback interface.
In this class of teachers, you can also call a method of the student class, of course, in this case, do not call the Student class method,
Instead of just implementing a callback function, tell the student that his homework was received.
Interface callbacks and upward transformation:
Upward transformation: For example: Shape s=new Circle ();
Here a Circle object is created and the resulting reference is immediately assigned to shape, and through inheritance, circle is a shape.
If you call the base class method (which is already overridden in the same way) S.draw (), the Circle.draw () method is called correctly due to late binding (polymorphic).
Cross-class references in Java
In Java, there are two rules for cross-class references that should be remembered:
1. If a is a reference to Class A, then a can point to an instance of Class A. Or point to a subclass of Class A. This is a case of upward transformation.
2. If a is a reference to interface A, then a must point to an instance of a class that implements interface A, which is the case of an interface callback.
The upward transformation in Java is automatic, but a downward transition requires a cast.
Interface callbacks in Java