1

I understand an interface is a contract and if a class implements that interface, it must define those abstract methods from the interface. What I don't understand is, how is data passed between two classes that use an interface?

So for example: In Android, a Fragment has an interface, say OnFragmentInteractionListener. At someplace in the code it calls,

onFragmentInteractionListener.displaySomeMessage(message);

The Activity will implement:

void displaySomeMessage(String message) {
     System.out.println(message);
}

How is "message" actually passed to the Activity? How does it retrieve this specific piece of data? I use interfaces all the time and know how to use them. But I just don't quite understand what the "contract" is actually doing behind the scenes, so that everyone uses the same data.

  • 1
    The runtime knows how to resolve onFragmentInteractionListener to a specific implementation (otherwise you'd get a resolution exception). From there it works like any other parameter reference. baeldung.com/java-stack-heap – Dan Wilson Apr 28 at 19:55
  • By the time the method gets called it is just plain old subroutine calling with arguments being pushed on a stack, the program counter being moved to the start of the routine and arguments being popped from the stack. The concept of an interface is meaningless at that level. It is a compiler level constraint that helps you write code. To the processor there is no interface or class or whatever OO concept. – Martin Maat Apr 28 at 20:50
  • 1
    onFragmentInteractionListener is an object (an instance). It's type is the OnFragmentInteractionListener interface, but the actual underlying variable is the instance of the activity. You are literally just calling activity.displaySomeMessage(message), except that the variable has a different name, and a different type. This is possible because the activity implements that interface, and therefore is also of that type, by virtue of type inheritance. Somewhere in the activity, you passed it to the fragment using fragment.setOnFragmentInteractionListener(this). – Filip Milovanović Apr 29 at 1:08
  • 1
    So there's no too much magic about it. The activity just passes itself to the fragment so that the fragment can call it later on when the interaction happens. The neat thing is that the fragment can accept anything that implements that interface - it doesn't have to be the activity. You can have the fragment notify something else. – Filip Milovanović Apr 29 at 1:12
  • @FilipMilovanović wow, that's super simple but actually makes a lot of sense. Thanks! – Peter G. Williams Apr 29 at 14:07
2

Typically, a function pointer is stored in a data structure like a virtual method table for each object. When it gets called, it first looks up the pointer to displaySomeMessage from the table, then it calls it like any other function. How arguments are passed to a function depends on the calling convention. To pass a string argument, you would usually either push its pointer onto the stack or store it in a register, then jump to the function pointer.

Learning to use GObject for OOP in C would help you understand virtual method tables. Learning assembly would help you understand calling conventions.

| improve this answer | |
1

An interface does two things, both of which help you in writing valid code.

On the one hand, an interface is a promise to the objects/classes that use the interface (like a Fragment) that whatever class implements the interface, it will have the methods declared in the interface and they will accept the arguments as declared in the interface. This promise helps the compiler to generate the correct code for calling that interface and it helps the IDE in giving you completion features so you don't have to remember the names yourself.

On the other hand, to the classes implementing an interface it is a reminder that they must offer certain methods with certain arguments and the compiler will check that you do. That is essentially what makes interfaces a "contract".

How interfaces work on a technical level (how the compiler can call a method in an unknown class and so on) is explained in the answer by @KarlBielefeldt,

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.