Java Interface used with Callbacks.
Java Interface as Callbacks
- Callbacks are methods that will be invoked when an event of a kind happens.
- It could be for instance a method that you have to put code in when a button is pressed.
-
You have to build your own callbacks in Java.
Callback method example:
interface TextReceiver { void receiveText(String text); } class TickerTape implements TextReceiver { public void receiveText(String text) { System.out.println("TICKER: " + text); } } public class TextSource { // Reference to the implemented Interface TextReceiver receiver; TextSource(TextReceiver r) { receiver = r; } public void sendText(String s) { receiver.receiveText(s); } public static void main(String[] args) { // Create a object of the class that implements the interface TickerTape tickerTape = new TickerTape(); TextSource myCar = new TextSource(tickerTape); // Send a text message, which is our event. myCar.sendText("Hallo"); } }
The result of this is:TICKER: Hallo
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.