http://www.artima.com/underthehood/invocationP.html
上記のリンクは、私の質問に明確に対応する貴重な例を示しています。
class Superclass {
private void interestingMethod() {
System.out.println("Superclass's interesting method.");
}
void exampleMethod() {
interestingMethod();
}
}
class Subclass extends Superclass {
void interestingMethod() {
System.out.println("Subclass's interesting method.");
}
public static void main(String args[]) {
Subclass me = new Subclass();
me.exampleMethod();
}
}
上記で定義したようにサブクラスで main() を呼び出すと、「スーパークラスの興味深いメソッド」が出力される必要があります。invokevirtual を使用すると、「サブクラスの興味深いメソッド」が出力されます。なんで?仮想マシンは、サブクラスであるオブジェクトの実際のクラスに基づいて、呼び出す InterestedMethod() を選択するためです。したがって、サブクラスのinterestingMethod()を使用します。一方、invokespecial を使用すると、仮想マシンは参照の型に基づいてメソッドを選択するため、スーパークラスのバージョンの InterestingMethod() が呼び出されます。