recipe № 030

Abstract method and native implementation

It turns out that JNI provides really cool feature – native implementation of abstract method. It allows to provide different native implementations for the same method, each implemented in different native library.

I guess it’s quite useful in case you want to implement: multiple native interfaces, you have different native codes and you want to choose between different implementations during run time, etc.

Basically, schema follows

In fact, it turns out that implementation is super simple

what we can do, is to provide different implementation (native ones) in classes that are subclassing from from AbstractHelloWorld.

That’s it. We can now easily switch between native implementations like this:

You can find full sample here: recipe № 029

Sources:

1. Native implementation of an abstract method
2. No Best Practices