はじめに
この実験では、Java で instanceof キーワードを使用して、オブジェクトが特定のクラスまたはインターフェースのインスタンスであるかどうかをチェックする方法を学びます。基本的な使い方を探索し、サブクラスでの動作をテストし、null オブジェクトの扱いを理解します。
実践的な例を通じて、オブジェクト指向プログラミングの基本概念である実行時型チェックを行うために instanceof を使用する実践的な経験を積むことができます。
instanceof を使用したクラスチェック
このステップでは、Java の instanceof キーワードを探索します。instanceof キーワードは、オブジェクトが特定のクラスのインスタンスであるか、または特定のインターフェースを実装しているかをテストするために使用されます。これは、実行時にオブジェクトの型をチェックするための便利なツールです。
instanceof がどのように機能するかを示すために、簡単な Java プログラムを作成しましょう。
WebIDE を開き、
~/projectディレクトリにいることを確認します。ターミナルのプロンプトを見るか、pwdと入力して Enter キーを押すことで確認できます。~/projectディレクトリにTypeCheck.javaという名前の新しい Java ファイルを作成します。左側のファイルエクスプローラーで右クリックして「New File」を選択し、TypeCheck.javaと入力することで作成できます。エディタで
TypeCheck.javaファイルを開き、以下のコードを貼り付けます。class Animal { // Base class } class Dog extends Animal { // Subclass of Animal } class Cat extends Animal { // Subclass of Animal } public class TypeCheck { public static void main(String[] args) { Animal myAnimal = new Dog(); // Check if myAnimal is an instance of Dog if (myAnimal instanceof Dog) { System.out.println("myAnimal is an instance of Dog"); } else { System.out.println("myAnimal is not an instance of Dog"); } // Check if myAnimal is an instance of Cat if (myAnimal instanceof Cat) { System.out.println("myAnimal is an instance of Cat"); } else { System.out.println("myAnimal is not an instance of Cat"); } // Check if myAnimal is an instance of Animal if (myAnimal instanceof Animal) { System.out.println("myAnimal is an instance of Animal"); } else { System.out.println("myAnimal is not an instance of Animal"); } } }このコードでは、以下のことを行っています。
- 基底クラス
Animalと 2 つのサブクラスDogとCatを定義しています。 mainメソッドでは、Animal型の変数myAnimalを作成し、Dogオブジェクトを代入しています。これは、DogがAnimalの一種であるため可能です。- その後、
instanceofキーワードを使用して、myAnimalがDog、Cat、Animalのインスタンスであるかどうかをチェックしています。
- 基底クラス
TypeCheck.javaファイルを保存します(Ctrl+S または Cmd+S)。WebIDE の下部にあるターミナルを開き、以下のコマンドを実行して Java プログラムをコンパイルします。
javac TypeCheck.javaエラーがなければ、このコマンドにより
~/projectディレクトリにTypeCheck.class、Animal.class、Dog.class、Cat.classファイルが作成されます。以下のコマンドを使用してコンパイルされたプログラムを実行します。
java TypeCheck以下のような出力が表示されるはずです。
myAnimal is an instance of Dog myAnimal is not an instance of Cat myAnimal is an instance of Animalこの出力は、
Dogオブジェクトを保持するmyAnimalが実際にDogのインスタンスであり、そのスーパークラスAnimalのインスタンスでもあるが、Catのインスタンスではないことを確認しています。
あなたは、Java で instanceof キーワードを使用してオブジェクトの型をチェックすることに成功しました。次のステップでは、instanceof がサブクラスとどのように動作するかを探索します。
サブクラスでのテスト
前のステップで、オブジェクトは自身のクラスのインスタンスであると同時に、そのスーパークラスのインスタンスでもあると見なされることを確認しました。ここでは、instanceof が異なるオブジェクト型とそのサブクラスでどのように動作するかをさらに探索します。
既存の TypeCheck.java ファイルを修正して、Cat オブジェクトで instanceof をテストします。
WebIDE のエディタで
TypeCheck.javaファイルを開きます。mainメソッドを見つけ、既存のmyAnimalのチェックの後に以下のコード行を追加します。System.out.println("\n--- Testing with a Cat object ---"); Animal anotherAnimal = new Cat(); // Check if anotherAnimal is an instance of Dog if (anotherAnimal instanceof Dog) { System.out.println("anotherAnimal is an instance of Dog"); } else { System.out.println("anotherAnimal is not an instance of Dog"); } // Check if anotherAnimal is an instance of Cat if (anotherAnimal instanceof Cat) { System.out.println("anotherAnimal is an instance of Cat"); } else { System.out.println("anotherAnimal is not an instance of Cat"); } // Check if anotherAnimal is an instance of Animal if (anotherAnimal instanceof Animal) { System.out.println("anotherAnimal is an instance of Animal"); } else { System.out.println("anotherAnimal is not an instance of Animal"); }これで、完全な
TypeCheck.javaファイルは以下のようになります。class Animal { // Base class } class Dog extends Animal { // Subclass of Animal } class Cat extends Animal { // Subclass of Animal } public class TypeCheck { public static void main(String[] args) { Animal myAnimal = new Dog(); // Check if myAnimal is an instance of Dog if (myAnimal instanceof Dog) { System.out.println("myAnimal is an instance of Dog"); } else { System.out.println("myAnimal is not an instance of Dog"); } // Check if myAnimal is an instance of Cat if (myAnimal instanceof Cat) { System.out.println("myAnimal is an instance of Cat"); } else { System.out.println("myAnimal is not an instance of Cat"); } // Check if myAnimal is an instance of Animal if (myAnimal instanceof Animal) { System.out.println("myAnimal is an instance of Animal"); } else { System.out.println("myAnimal is not an instance of Animal"); } System.out.println("\n--- Testing with a Cat object ---"); Animal anotherAnimal = new Cat(); // Check if anotherAnimal is an instance of Dog if (anotherAnimal instanceof Dog) { System.out.println("anotherAnimal is an instance of Dog"); } else { System.out.println("anotherAnimal is not an instance of Dog"); } // Check if anotherAnimal is an instance of Cat if (anotherAnimal instanceof Cat) { System.out.println("anotherAnimal is an instance of Cat"); } else { System.out.println("anotherAnimal is not an instance of Cat"); } // Check if anotherAnimal is an instance of Animal if (anotherAnimal instanceof Animal) { System.out.println("anotherAnimal is an instance of Animal"); } else { System.out.println("anotherAnimal is not an instance of Animal"); } } }修正した
TypeCheck.javaファイルを保存します。ターミナルでプログラムを再度コンパイルします。
javac TypeCheck.javaコンパイルしたプログラムを実行します。
java TypeCheckこれで、以下のような出力が表示されるはずです。
myAnimal is an instance of Dog myAnimal is not an instance of Cat myAnimal is an instance of Animal --- Testing with a Cat object --- anotherAnimal is not an instance of Dog anotherAnimal is an instance of Cat anotherAnimal is an instance of Animalこの出力は、
Catオブジェクトを保持するanotherAnimalがCatとAnimalのインスタンスであるが、Dogのインスタンスではないことを示しています。これにより、instanceofがオブジェクトの実際の型とその継承階層をチェックするという概念が強化されます。
あなたは、異なるサブクラスのオブジェクトで instanceof キーワードを正常にテストしました。次のステップでは、instanceof が null オブジェクトでどのように動作するかを見ていきます。
null オブジェクトでの検証
この最後のステップでは、テスト対象のオブジェクトが null の場合に instanceof キーワードがどのように動作するかを調査します。これを理解することは、Java プログラムで潜在的なエラーを回避するために重要です。
再度 TypeCheck.java ファイルを修正して、null オブジェクトでのテストを追加します。
WebIDE のエディタで
TypeCheck.javaファイルを開きます。mainメソッドの末尾、前のテストの後に以下のコード行を追加します。System.out.println("\n--- Testing with a null object ---"); Animal nullAnimal = null; // Check if nullAnimal is an instance of Dog if (nullAnimal instanceof Dog) { System.out.println("nullAnimal is an instance of Dog"); } else { System.out.println("nullAnimal is not an instance of Dog"); } // Check if nullAnimal is an instance of Animal if (nullAnimal instanceof Animal) { System.out.println("nullAnimal is an instance of Animal"); } else { System.out.println("nullAnimal is not an instance of Animal"); }これで、完全な
TypeCheck.javaファイルは以下のようになります。class Animal { // Base class } class Dog extends Animal { // Subclass of Animal } class Cat extends Animal { // Subclass of Animal } public class TypeCheck { public static void main(String[] args) { Animal myAnimal = new Dog(); // Check if myAnimal is an instance of Dog if (myAnimal instanceof Dog) { System.out.println("myAnimal is an instance of Dog"); } else { System.out.println("myAnimal is not an instance of Dog"); } // Check if myAnimal is an instance of Cat if (myAnimal instanceof Cat) { System.out.println("myAnimal is not an instance of Cat"); } else { System.out.println("myAnimal is not an instance of Cat"); } // Check if myAnimal is an instance of Animal if (myAnimal instanceof Animal) { System.out.println("myAnimal is an instance of Animal"); } else { System.out.println("myAnimal is not an instance of Animal"); } System.out.println("\n--- Testing with a Cat object ---"); Animal anotherAnimal = new Cat(); // Check if anotherAnimal is an instance of Dog if (anotherAnimal instanceof Dog) { System.out.println("anotherAnimal is not an instance of Dog"); } else { System.out.println("anotherAnimal is not an instance of Dog"); } // Check if anotherAnimal is an instance of Cat if (anotherAnimal instanceof Cat) { System.out.println("anotherAnimal is an instance of Cat"); } else { System.out.println("anotherAnimal is an instance of Cat"); } // Check if anotherAnimal is an instance of Animal if (anotherAnimal instanceof Animal) { System.out.println("anotherAnimal is an instance of Animal"); } else { System.out.println("anotherAnimal is an instance of Animal"); } System.out.println("\n--- Testing with a null object ---"); Animal nullAnimal = null; // Check if nullAnimal is an instance of Dog if (nullAnimal instanceof Dog) { System.out.println("nullAnimal is an instance of Dog"); } else { System.out.println("nullAnimal is not an instance of Dog"); } // Check if nullAnimal is an instance of Animal if (nullAnimal instanceof Animal) { System.out.println("nullAnimal is an instance of Animal"); } else { System.out.println("nullAnimal is not an instance of Animal"); } } }修正した
TypeCheck.javaファイルを保存します。ターミナルでプログラムをコンパイルします。
javac TypeCheck.javaコンパイルしたプログラムを実行します。
java TypeCheck以下のような出力が表示されるはずです。
myAnimal is an instance of Dog myAnimal is not an instance of Cat myAnimal is an instance of Animal --- Testing with a Cat object --- anotherAnimal is not an instance of Dog anotherAnimal is an instance of Cat anotherAnimal is an instance of Animal --- Testing with a null object --- nullAnimal is not an instance of Dog nullAnimal is not an instance of Animal出力からわかるように、
instanceofでテストするオブジェクトがnullの場合、結果は常にfalseになります。これは、予期しない動作やNullPointerExceptionを回避するためにinstanceofを使用する際に覚えておく重要なポイントです。
あなたは、instanceof キーワードが null オブジェクトでどのように動作するかを正常に検証しました。これで instanceof キーワードの探索は終了です。
まとめ
この実験では、Java の instanceof キーワードを使用して、オブジェクトが特定のクラスのインスタンスであるか、または特定のインターフェースを実装しているかをチェックする方法を学びました。基底クラスとサブクラスを持つ簡単なプログラムを作成し、instanceof を使用して実行時にオブジェクトの型を検証することで、その使い方を実証しました。
さらに、instanceof がサブクラスでどのように動作するかを調べ、オブジェクトは自身のクラスのインスタンスであると同時に、すべてのスーパークラスのインスタンスと見なされることを確認しました。最後に、instanceof を null オブジェクトに適用した場合の動作を調べ、このような場合には常に false を返すことを理解しました。



