Given the following interface and implementation class:
public interface MyInterface {
public String getSomething ();
}
public class MyImplementation implements MyInterface {
@Override
public String getSomething () {
return "Someting";
}
public String getOtherThing () {
return "otherThing";
}
}
And then some client code:
MyInterface objectViaMyInt = new MyImplementation();
objectViaMyInt.getSometing(); //WORKS, OF COURSE
objectViaMyInt.getOtherThing(); //DOESNT WORK, OF COURSE
objectViaMyInt.toString(); // WORKS TOO
So, I think I understand the spirit of this . .. every object is an Object and should inherit those essential methods. But how does this actually work? It seems that it can’t follow the normal rules for inheritance and interface ( type ) based access . . .
At a language level, it works because JLS section 9.2 says it does:
At a JVM level, the JVM can just use the knowledge that every instance of an interface must obviously be an object, so it can get at the right member implementations in the normal way.