Page 1 of 1

About Question enthuware.ocajp.i.v7.2.837 :

Posted: Sat Mar 09, 2013 3:53 am
by The_Nick
Hi,
Can we actually say, as a result of this question, that the operator "instanceof" works at compilation time for classes and at runtime for interfaces?

Thanks in advance.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.837 :

Posted: Sat Mar 09, 2013 7:15 am
by admin
Partially true. The compilation fails in some cases only because the compiler is able to figure out that the reference can never point to an object of the class on the right hand side. But the real type checking happens at run time.

With interfaces, it is always possible (because any class can implement multiple interfaces) so it doesn't do any type checking at compile time.

Re: About Question enthuware.ocajp.i.v7.2.837 :

Posted: Sat Mar 09, 2013 8:07 am
by The_Nick
admin wrote:Partially true. The compilation fails in some cases only because the compiler is able to figure out that the reference can never point to an object of the class on the right hand side. But the real type checking happens at run time.

With interfaces, it is always possible (because any class can implement multiple interfaces) so it doesn't do any type checking at compile time.

OK So basically we can say that for interfaces the checking of whether is an instance or not it's made at runtime. The compiler cannot see that the other class implements a certain interface? On the other hand it does so with classes, the compiler is able to see whether or not a class is "of the same family" so why sh
uld not it see that it implements an interface?

Re: About Question enthuware.ocajp.i.v7.2.837 :

Posted: Sat Mar 09, 2013 8:24 am
by admin
Because it is possible that a new class that implements that interface may become available and the reference may point to an instance (which can be created using reflection) of that class.

Re: About Question enthuware.ocajp.i.v7.2.837 :

Posted: Thu Jan 15, 2015 2:06 pm
by Area_Man
With very similar code I get an error message "inconvertible types". Specifically, when I apply instanceof to an instance of a class that has no ancestors other than Object and evaluate it to see if it is an instance of any other class type I get this error. I have compiled and run your code on my machine and it works as advertised. I don't understand why (b instanceof Flyer) doesn't produce the "inconvertible types" error. Any help you made provide is greatly appreciated. Thanks.

Re: About Question enthuware.ocajp.i.v7.2.837 :

Posted: Thu Jan 15, 2015 9:40 pm
by admin
Please read my post just above yours. It explains why a compiler may not be able to generate an error with an interface.

Re: About Question enthuware.ocajp.i.v7.2.837 :

Posted: Mon Dec 11, 2017 6:19 pm
by philippe
In order to understand this topic better, I have written the following code example in order to prove it's a good thing Java does not check for "incompatible types" in case of an instanceof of an interface:

Code: Select all

interface Flyer{ }
class Bird implements Flyer { }
class Eagle extends Bird { }
class Bat { }
class SubBat extends Bat implements Flyer {}

class TestClass {

    public static void main(String[] args) {
        Flyer f = new Eagle();
        Eagle e = new Eagle();
        Bat b = new SubBat();
        
        if(f instanceof Bird) System.out.println("f is a Bird");
        if(e instanceof Flyer) System.out.println("e is a Flyer");
        if(b instanceof Flyer) System.out.println("b is a Flyer");
    }
}
Result:

Code: Select all

f is a Bird
e is a Flyer
b is a Flyer