When run, the following program will print all the letters Interface1, Interface2, MyClass1, and MyClass2.
True or false?.
interface Interface1{} interface Interface2{} class MyClass1 implements Interface1 {} class MyClass2 extends MyClass1 implements Interface2 {} public class Main { public static void main(String[] args) { Interface1 x = new MyClass2(); if (x instanceof Interface1) System.out.println("Interface1"); if (x instanceof Interface2) System.out.println("Interface2"); if (x instanceof MyClass1) System.out.println("MyClass1"); if (x instanceof MyClass2) System.out.println("MyClass2"); }//w ww. j a va 2 s. co m }
Select the one correct answer.
(a)
The program will print all the letters Interface1, Interface2, MyClass1, and MyClass2, when run.
The object referred to by the reference x is of class MyClass2.
Class MyClass2 extends class MyClass1 and class MyClass1 implements interface Interface1.
This makes Interface1, Interface2, and MyClass1 supertypes
of class MyClass2.
The reference value of an object of class MyClass2 can be assigned to any reference of its super types and is, therefore, an instanceof these types.