Java - callback

In C/ C++, callback refers to the mechanism of calling a function from another function. Memory address of a function is represented as function pointer here. SO the callback is achieved by passing the pointer of func1() to func2().

However, in java, there is no function pointer existing. And we use a callback object or a callback interface, and the interface is passed that refers to the location of a function.

Below is an example to compute tax by state tax and fedaral tax. Suppose federal tax keeps same while state tax vary by state. We can build interface and implements interface to realize it.
// Java program to demonstrate callback mechanism
// using interface is Java

// Create interface 
import java.util.Scanner; 
interface STax { 
    double stateTax(); 

// Implementation class of Punjab state tax 
class Punjab implements STax { 
    public double stateTax() 
        return 3000.0; 

// Implementation class of Himachal Pardesh state tax 
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.out.println("Enter the state name"); 
        String state =; // name of the state 

        // The state name is then stored in an object c 
        Class c = Class.forName(state); 

        // Create the new object of the class whose name is in c 
        // Stax interface reference is now referencing that new object 
        STax ref = (STax)c.newInstance(); 

        /*Call the method to calculate total tax 
        and pass interface reference - this is callback . 
        Here, ref may refer to stateTax() of Punjab or HP classes 
        depending on the class for which the object is created 
        in the previous step 

    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); 


文章标题:Java - callback


本文作者:Leilei Chen

发布时间:2020-02-06, 10:57:58

最后更新:2020-02-06, 10:58:23


版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。