How does Java call the function of the object?

advertisements

From what I read before, after .java file compiles to .class files, every object is simply Object after erasure. Such as

Foo f = new Foo();

Compiles to .class file, and decompile, it becomes:

Object f = new Foo();

So how does JRE call the function of an object when running? Where is the function stored in the memory? Inside the object? Or with a hierarchy of class structure and does lookup up the hierarchy?


According to the Java spec and wikipedia

There are 10 basic sections to the Java Class File structure:

  • Magic Number: 0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file
  • Constant Pool: Pool of constants for the class
  • Access Flags: for example whether the class is abstract, static, etc.
  • This Class: The name of the current class
  • Super Class: The name of the super class
  • Interfaces: Any interfaces in the class
  • Fields: Any fields in the class
  • Methods: Any methods in the class
  • Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)

At run time, the type of the object is retrieved, its class file (or rather the virtual method table) is checked for an implementation of the method invoked. If that class doesn't have such an implementation, the parent class is checked (retrieved from super class entry), and so on, eventually failing if none is found.