Given:
2. class Shape { } 3. interface Printable { } 4. class MyClass implements Printable { } 5. public class Main extends MyClass { 6. public static void main(String[] args) { 7. Printable af = new MyClass(); 8. MyClass ff = new MyClass(); 9. Main b = new Main(); 10. Shape r = new Shape(); 11. if(af instanceof Printable) System.out.print("1 "); 12. if(af instanceof Main) System.out.print("2 "); 13. if(b instanceof Printable) System.out.print("3 "); 14. if(ff instanceof Main) System.out.print("4 "); 15. if(r instanceof Printable) System.out.print("5 "); 16. } /*from www . j a va 2 s . c o m*/ 17. }
What is the result?
B is correct.
Line 12 and line 14s' instanceof tests fail because Main is not a superclass of either af or ff's respective objects.
Line 15 compiles because it's possible that Shape could, at some point, implement Printable.