I am in the process of learning how JNI works for calling C++ object methods from a Java program. I am working with the Android NDK/SDK to write an app.
I am a beginner with JNI and i would like to know if polymorphism is supported. That is, suppose i have a simple C++ class:
class HeyBoy
{
public:
virtual int getValue()
{
return 10;
}
};
class HeyBoyDerived1 : public HeyBoy
{
public:
int getValue()
{
return 20;
}
}
class HeyBoyDerived2 : public HeyBoy
{
public:
int getValue()
{
return 30;
}
}
At run-time, i want the user to select which implementation to use (1 or 2), just pressing a button on the app interface. The right C++ object should be created.
Then, i would place a button with a label ‘Get Value’. When the button is pressed, the getValue() method of the current selected implementation should be called. After that, the result is printed on the phone screen.
I would really like to see some code examples!
Thank you.
The short answer is NO. JNI is a C interface, it’s not aware of C++, classes, inheritance, or polymorphism.
The longer answer is: yes, C functions can be used to call the required constructors and invoke virtual methods.