Search code examples
dalviksmali

invoke-super <interface method> vs invoke-interface <interface method>?


In Dex versions 037+, what is the difference in behavior between invoke-super mid and invoke-interface mid where mid is an interface method?

I am quoting the following from the Dalvik docs [1], but I am unable to make much sense out of it:

In Dex files version 037 or later, if the method_id refers to an interface method, invoke-super is used to invoke the most specific, non-overridden version of that method defined on that interface. The same method restrictions hold as for invoke-virtual. In Dex files prior to version 037, having an interface method_id is illegal and undefined.

invoke-interface is used to invoke an interface method, that is, on an object whose concrete class isn't known, using a method_id that refers to an interface.

[1] https://source.android.com/devices/tech/dalvik/dalvik-bytecode


Solution

  • Dex version 37 adds support for default interface methods. So if you call invoke-super with something like Lmy/interface;->someMethod()V, it will search through the interface hierarchy to find the first interface that provides an implementation of that interface.

    So, for a fuller example, let's say we have a few classes.

    • Lmy/superinterface;
    • Lmy/interface; which extends Lmy/superinterface;
    • Lmy/superclass; which implements Lmy/interface;
    • Lmy/subclass; which extends Lmy/superclass;

    Let's say that Lmy/superinterface; defines a default implementation of someMethod()V, and both Lmy/superclass; and Lmy/subclass; provide their own implementation

    If you have an object of Lmy/subclass; in v0, then

    • invoke-super {v0}, Lmy/interface;->someMethod()V invokes the implementation from Lmy/superinterface;
    • invoke-super {v0}, Lmy/subclass;->someMethod()V invokes the implementation from Lmy/superclass;
    • invoke-virtual {v0}, Lmy/subclass;->someMethod()V invokes the implementation from Lmy/subclass;