Java Magazine, Sept/Oct 2017
Bear groom Furry groom ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 48 java 9 were known as klassOops In Java 8 and Java 9 the klass metadata was moved out of the Java heap into the native heap so the object headers are no longer required Some of the information from the klass is available to Java programmers via the Class object corresponding to the type but they are separate concepts One of the most important areas of the klass is the vtable This area is essentially a table of function pointers that point to the implementations of methods defined by the type When an instance method is called via invokevirtual the JVM consults the vtable to see exactly which code needs to be executed If a klass does not have a definition for the method the JVM follows a pointer to the klass corresponding to the superclass and tries again This process is the basis of method overriding in the JVM To make the process eficient the vtables are laid out in a specific way Each klass lays out its vtable so that the first methods to appear are the methods that the parent type defines These methods are laid out in the exact order that the parent type used The methods that are new to this type and are not declared by the parent class come at the end of the vtable This means that when a subclass overrides a method it will be at the same ofset in the vtable as the implementation being overridden This makes the lookup of overridden methods completely trivial because their offset in the vtable will be the same as the ofset of their parent Figure 1 shows an example defined by the classes Pet Cat and Bear and the interface Furry Object Pet feed Cat groom Figure 1 Simple inheritance hierarchy purr
You must have JavaScript enabled to view digital editions.