Consider the following code:
interface Cute{ } class Pet implements Cute { } class Cat extends Pet { } class Bat { } /*from w w w . ja v a2 s . co m*/ public class Main { public static void main (String [] args) { Cute f = new Cat (); Cat e = new Cat (); Bat b = new Bat (); if (f instanceof Cute) System.out.println ("f is a Cute"); if (e instanceof Pet) System.out.println ("e is a Pet"); if (b instanceof Pet) System.out.println ("f is a Pet"); } }
What will be printed when the above code is compiled and run?
Select 1 option
Correct Option is : A
There is no compilation issue with b instanceof Cute because Cute is an interface.
It is possible for b to point to an object of a class that is a sub class of Bat and also implements Cute.
So the compiler doesn't complain.
If you make Bat class as final, b instanceof Cute will not compile because the compiler knows that it is not possible for b to point to an object of a class that implements Cute.