Search code examples
objectassemblyinterfacelinkercompiler-construction

How are interfaces represented at a lower/assembly level?


I understand objects are just chunks of data, where the first member variable starts at the exact same location as the object. For example, an object whose pointer points to location 0x0100, will also have the first member variable located at 0x0100.

But how do interfaces in a language like Java, which have no implementation, work? These are purely abstract types.

As in, how does a CPU or compiler know to interpret Object A as Interface X, or Object B as Interface X and Y depending on the context? Is this something done at the compiler level, that has no bearing on the lower level interpretations?


Solution

  • You need to understand that in languages like Java & C#, the first member of an instance is a pointer to a class-shared object; this object participates in identifying the actual class that was instantiated, in checking down casts, in determining the overrides to call for virtual method invocations, and in determining the implementation to use for interface method invocations.

    But how do interfaces in a language like Java, which have no implementation, work? These are purely abstract types.

    Sure interfaces themselves are "abstract", but the interface methods are always invoked on instances of concrete classes (that implement the interfaces), so that's where implementation comes in.

    The language treats interfaces, similar to classes (both abstract and concrete), assigning their methods to indexes, and using those method indexes in lookup (at the class-shared object of the instance) to find the actual implementation to invoke.

    So, key is that the interface methods are given positions in the array of method references within the class-shared object of each instantiable type (that implements the interface), such that each class could have a different implementation of an interface method.