Callback using Interfaces in Java – GeeksforGeeks

Callback in C/C++ : The mechanism of calling a function from another function is called “callback”. Memory address of a function is represented as ‘function pointer’ in the languages like C and C++. So, the callback is achieved by passing the pointer of function1() to function2().
Callback in Java : But the concept of a callback function does not exist in Java because Java doesn’t have pointer concept. However, there are situations where one could speak of a callback object or a callback interface. Instead of passing the memory address of a function, interface is passed that refers to the location of a function.
 

Example

Let us take an example to understand where callbacks can be used. Suppose a programmer wants to design a tax calculator that calculates total tax of a state. Assume there are only two taxes, central and state tax. Central tax is common whereas the state tax varies from one state to another. The total tax is the sum of the two. Here separate method like stateTax() is implemented for every state and call this method from another method calculateTax() as:
 

static void calculateTax(address of stateTax() function)
{
    ct = 1000.0
    st = calculate state tax depending on the address
    total tax = ct+st;
}

In the preceding code, the address of stateTax() is passed to calculateTax(). The calculateTax() method will use that address to call stateTax() method of a particular state and the state tax ‘st’ is calculated. 
Since the code of stateTax() method changes from one state to another state, it is better to declare it as an abstract method in an interface, as: 
 

interface STax
{
     double stateTax();
}

The following is the implementation of stateTax() for Punjab state: 
 

class Punjab implements STax{
    public double stateTax(){
    return 3000.0;
     }
}

The following is the implementation of stateTax() for HP state: 
 

class HP implements STax
{
    public double stateTax()
    {
        return 1000.0;
    }
}

Now, the calculateTax() method can be designed as: 
 

static void calculateTax(STax t)
{
    // calculate central tax
    double ct = 2000.0;

    // calculate state tax
    double st = t.stateTax();
    double totaltax = st + ct;

    // display total tax
    System.out.println(“Total tax  =”+totaltax);
}

Here, observe the parameter ‘STax t’ in the calculateTax() method. ‘t‘ is a reference of the ‘STax’ interface which is passed as a parameter to the method. Using this reference, the stateTax() method is called, as: 
 

double st = t.stateTax();

Here, if ‘t’ refers to stateTax() method of class Punjab, then that method is called and its tax is calculated. Similarly, for class HP. In this way, by passing interface reference to calculateTax() method, it is possible to call stateTax() method of any state. This is called callback mechanism.
By passing the interface reference that refers to a method, it is possible to call and use that method from another method.
 

Java




 

import java.util.Scanner;

interface STax {

    double stateTax();

}

 

class Punjab implements STax {

    public double stateTax()

    {

        return 3000.0;

    }

}

 

class HP implements STax {

    public double stateTax()

    {

        return 1000.0;

    }

}

 

class TAX {

    public static void main(String[] args)

throws ClassNotFoundException, IllegalAccessException, InstantiationException

    {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the state name");

        String state = sc.next();

 

        

        Class c = Class.forName(state);

 

        

        

        STax ref = (STax)c.newInstance();

 

        

        

        

        

        

         

 

        calculateTax(ref);

    }

    static void calculateTax(STax t)

    {

        

        double ct = 2000.0;

 

        

        double st = t.stateTax();

        double totaltax = st + ct;

 

        

        System.out.println("Total tax =" + totaltax);

    }

}



Output: 
 

Enter the state name
Punjab
Total tax = 5000.0

References: 
How to implement callback functions in Java? 
Core Java: An Integrated Approach
 

My Personal Notes

arrow_drop_up