SoFunction
Updated on 2025-03-11

How to use interface object instantiation in interface callbacks

First, let’s clarify a problem, that is, interfaces can not only declare objects, but also instantiate objects! The function is shown below.

Interface callback:You can assign a reference to an object created by implementing a certain interface class to the interface variable declared by the interface, then

Interface variables can call methods in the interface implemented by the class. In fact, when the interface variable calls the interface implemented by the class

When the method in the process is notified to the corresponding object to call the interface method.

Let's look at the following example:

interface Computerable 
 
{ 
 
public double area(); 
 
} 
 
 
 
class Rec implements Computerable 
 
{ 
 
double a,b; 
 
Rec(double a,double b) 
 
{ 
 
 = a; 
 
 = b; 
 
} 
 
public double area() { 
 
return (a*b); 
 
} 
 
} 
 
 
 
class Circle implements Computerable 
 
{ 
 
double r; 
 
Circle(double r) 
 
{ 
 
 = r; 
 
} 
 
public double area() { 
 
return (3.14*r*r); 
 
} 
 
} 
 
 
 
class Volume 
 
{ 
 
Computerable bottom; 
 
double h; 
 
Volume(Computerable bottom, double h) 
 
{ 
 
 = bottom; 
 
 = h; 
 
} 
 
 
 
public void changeBottome(Computerable bottom) 
 
{ 
 
 = bottom; 
 
} 
 
 
 
public double volume() 
 
{ 
 
return (()*h/3.0); 
 
} 
 
} 
 
 
 
public class InterfaceRecall { 
 
public static void main(String[] args) 
 
{ 
 
Volume v = null; 
 
Computerable bottom = null; 
 
 
 
//The excuse variable stores a reference to the method that implements the interface in the object. 
bottom = new Rec(3,6); 
 
("The area of ​​a rectangle is:"+()); 
 
v = new Volume(bottom, 10); 
 
//The volume method of the volume class instance actually calculates the volume of the rectangle, the same below 
("The volume of the prism is:"+()); 
 
 
 
bottom = new Circle(5); 
 
("The area of ​​a circle is:"+()); 
 
(bottom); 
 
("The volume of the cylinder is:"+()); 
 
 
 
} 
 
}

Output:

The area of ​​the rectangle is: 18.0

The volume of the prism is: 60.0

The area of ​​the circle is: 78.5

The volume of the cylinder is: 261.66666666666666667

Through the above example, it is not difficult to see that the instantiation of an interface object is actually an interface object as a reference, pointing to all methods in the class that implements its method. This is very similar to a function pointer in C++, but there is a difference. Interface object instantiation in java is actually one-to-many (bottom can still be called if Computerable has other methods), while function pointers in C++ are one-to-one. However, it should be noted that the instantiation of an interface object must be instantiated by the class that implements it, and cannot be instantiated by the interface itself. Instantiating its own objects with the interface itself is not allowed in Java.

The above method of using instantiation of interface objects in interface callbacks is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.