At a high level, I understand that an interface is a way of saying
Just do this, I don't care how
In other words, it basically allows you to program declaratively instead of imperatively. You make your code more flexible when parts of it can tell other parts of it what to do, not how to do it.
I grok that concept when it comes to high-level languages like Java.
However, I am curious how compilers deal with interfaces. This may be a bigger question than I realize.
Here's my theory: It's based upon an offset in RAM.
As an example, when an object is created that implements the ICompare
interface which includes the Compare
method, then the compiler knows to put that Compare
method at a 16 byte offset of where that object starts in RAM. In fact, every object that implements the ICompare
method has its Compare
method at the 16th byte. That way, when other code uses that interface to call the Compare
method, the system knows to perform the instructions at the 16th byte of that object.
Is that how a compiler handles interfaces (using a standard location in every object) or does it use some kind of lookup table to know where an object's Compare
method is? Or some other method entirely?