Class Part4_InterfaceMonomorphism

java.lang.Object
Part4_InterfaceMonomorphism

public class Part4_InterfaceMonomorphism extends Object
Demonstrates monomorphic dispatch: because PureBreedDog is final, the JIT can devirtualize the call and invoke speak() directly, without an itable/vtable lookup.

Disassembled bytecode (javap -c):

Compiled from "Part4_InterfaceMonomorphism.java"

public class Part4_InterfaceMonomorphism {
public Part4_InterfaceMonomorphism();Code:0:aload_0 1:invokespecial#1 // Method java/lang/Object."<init>":()V
    4:return

public static void directSpeak(PureBreedDog);
    Code:
         0: aload_0
         1: invokevirtual #7                  // Method PureBreedDog.speak:()V
         4: return

public static void main(java.lang.String[]);
    Code:
         0: new           #8                  // class PureBreedDog
         3: dup
         4: invokespecial #12                 // Method PureBreedDog."<init>":()V
         7: astore_1
         8: aload_1
         9: invokestatic  #13                 // Method directSpeak:(LPureBreedDog;)V
        12: return
}
  • Constructor Details

    • Part4_InterfaceMonomorphism

      public Part4_InterfaceMonomorphism()
  • Method Details

    • directSpeak

      public static void directSpeak(PureBreedDog explicitDog)
      Scenario A: Monomorphic target explicitly declared by final type reference.
      Parameters:
      explicitDog - concrete, non-overridable PureBreedDog reference.
    • main

      public static void main(String[] args)