The callback function refers to calling a function from another function. The callback() function is useful when there is a need to invoke a specific condition. Interfaces are used to implement the callback() function in Java. To achieve flexibility in codes, there arises the need to use a callback function in Java.
In this article, we will explain the callback method in detail.
How to Implement a Java Callback Function?
To implement a callback function in Java, the approach involves creating a function as a first-class entity, which is then invoked from a second class. Unlike certain programming languages such as C and C++, Java does not have a built-in callback() function due to the absence of pointers in its design.
The callback() functions in Java work in the following manner:
- Make an interface with the name A that contains one method that is X().
- Make a method and declare it as method 1 and pass X as a parameter.
- Now, call method X inside method 1.
- When calling method 1 the instance X is overridden by A.
Example 1: Callback Function
The below code example will emphasize in detail the working of the callback function in Java.
public class implementcallback {
public static void main(String args[]) {
//First Calling Function
function1(new firstinterface()
{
public String callingA()
{
//Return the output for this method
return "This is the first call ";
}
} ) ;
//Secind calling Function
function1(new firstinterface()
{
public String callingA() {
return "This is the second call";
}
} ) ;
//Third calling function
function1(new firstinterface()
{
public String callingA() {
return "This is the third call";
}
});
}
public static void function1(firstinterface intrA)
{
System.out.println("Calling Method here is : " + intrA.callingA());
}
public interface firstinterface {
public String callingA();
}
}
In the code above:
- There are three functions created that have the same calling object “intrA”.
- The return statements are different in each function.
- Using println() function displays/prints the desired output.
Output
In the above output, the statements are printed according to the input passed.
Example 2: Callback Function to Add the Values
In the following code, the callback function is used to sum the given values:
//To get the user input
import java.util.Scanner; //declare first interface
interface firstinterface {
double function1();
} //The implementation of first interface
class X implements firstinterface {
public double function1()
{
return 1800;
}
}
class Y implements firstinterface {
public double function1()
{
return 4000;
}
}
class Example2 {
//Class not found exception if the user enters the wrong value or is case sensitive.
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException
{ //The scanner for the user input
Scanner element = new Scanner(System.in);
//User input
System.out.println("Please enter the class name either X or Y: ");
String nameofclass = element.next();
Class nclass = Class.forName(nameofclass);
firstinterface intial = (firstinterface)nclass.newInstance();
function2(intial);
}
static void function2(firstinterface intial)
{
double x = 2000;
double y = intial.function1();
double sum = x + y;
//The sum according to the entering class
System.out.println("The total amount is :" + sum);
}
}
In the above code:
- The two classes X and Y have implemented the first interface.
- An integer value has been passed to the X and Y classes respectively.
- A class named Example 2 has been created that takes input from the user.
- An integer value for x has been passed in function 2.
- While Y has been kept as the initial of function 1.
- The output appears using the println() function according to the specified input class.
Output
The output demonstrates that upon entering the input as the “X” class, the program adds the value from the “class x” variable (which was 1800) to the value stored in the “double x” variable (which is 2000).
The output appears as 6000 when class Y is entered as input.
The article on the callback() function has come to an end.
Conclusion
The callback() function in Java is implemented in such a manner that a function is called from another function. The main advantage of using a callback() function in Java is that it has a flexible and modular approach. This article has described in detail the implementation of the callback() function in Java effectively.