Class Part3_InterfacePolymorphism

java.lang.Object
Part3_InterfacePolymorphism

public class Part3_InterfacePolymorphism extends Object
Demonstrates interface-based polymorphism resolved via the JVM's itable (interface method table) rather than a class vtable.

Disassembled bytecode (javap -c):

Compiled from "Part3_InterfacePolymorphism.java"

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

public static void makeSpeak(ISpeaker);
    Code:
         0: aload_0
         1: invokeinterface #7,  1            // InterfaceMethod ISpeaker.speak:()V
         6: return

public static void main(java.lang.String[]);
    Code:
         0: new           #12                 // class InterfaceDog
         3: dup
         4: invokespecial #14                 // Method InterfaceDog."<init>":()V
         7: astore_1
         8: new           #15                 // class InterfaceCat
        11: dup
        12: invokespecial #17                 // Method InterfaceCat."<init>":()V
        15: astore_2
        16: aload_1
        17: invokestatic  #18                 // Method makeSpeak:(LISpeaker;)V
        20: aload_2
        21: invokestatic  #18                 // Method makeSpeak:(LISpeaker;)V
        24: return
}
  • Constructor Details

    • Part3_InterfacePolymorphism

      public Part3_InterfacePolymorphism()
  • Method Details

    • makeSpeak

      public static void makeSpeak(ISpeaker s)
      Contractual Dispatch via Dynamic itable Lookups.
      Parameters:
      s - interface-typed reference; the concrete implementation invoked is resolved at runtime via invokeinterface.
    • main

      public static void main(String[] args)