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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick

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

Post 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

admin
Site Admin
Posts: 10055
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

The_Nick

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

Post 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?

admin
Site Admin
Posts: 10055
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Area_Man
Posts: 1
Joined: Thu Jan 15, 2015 1:46 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10055
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

philippe
Posts: 29
Joined: Sun Jul 16, 2017 4:24 pm
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: pavvel and 40 guests