Page 1 of 1
About Question enthuware.ocajp.i.v7.2.838 :
Posted: Wed Mar 28, 2012 6:02 am
by admin
Can you please have a read and tell me why option c is not correct? and I don't understand why you say a) is correct.
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Wed Mar 28, 2012 6:04 am
by admin
Hi, c is not correct because the given code will not compile and so it cannot print anything. Regd. why a is correct, try to answer this question: Is it at all possible for b to point to an object that is-a Bird? If you think it is possible, write it in code and check it again.
The compiler knows that it is not possible and so it knows that the line of statement if(b instanceof Bird) is logically invalid and so it refuses to compile.
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Fri May 31, 2013 5:26 pm
by Crashtest
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Tue May 13, 2014 11:16 am
by JeramieH
For me, the trivial part was recognizing that Bat is not a Bird. The unexpected part was not realizing that the compiler would proactively figure that out on its own and stop compiling. Sometimes the compiler checks are more aggressive than I expect from it.
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Tue Jun 17, 2014 10:16 pm
by Shortrope
I don't see any place in the question where a
Bat is trying to be a
Bird.
There is an
if condition, asking
if (b instanceof Bird)... but shouldn't this just resolve to false not a compilation error?
Is this the same code you are looking at? Or do I need to pound another RedBull?
Code: Select all
interface Flyer{ }
class Bird implements Flyer { }
class Eagle extends Bird { }
class Bat { }
public class TestClass {
public static void main(String[] args) {
Flyer f = new Eagle();
Eagle e = new Eagle();
Bat b = new Bat();
if(f instanceof Flyer) System.out.println("f is a Flyer");
if(e instanceof Bird) System.out.println("e is a Bird");
if(b instanceof Bird) System.out.println("f is a Bird");
}
}
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Tue Jun 17, 2014 10:24 pm
by admin
The fact that you are asking the compiler to accept the code b instanceof Bird, means that you think there is a possibility that b could point to an instance of Bird. Compiler figures out that it is absolutely impossible and thinks that you made a mistake and flags an error.
The underlying principle in many such seeming confusing things in Java is that common coding problems should be avoided as much as possible. Unreachable code is another such example. If the compiler finds out that a piece of code can never be executed, why let it be there? It must be a programmer's oversight and must be fixed right away.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Fri Jan 16, 2015 10:02 am
by Alina_Lapina
You are so nice, Paul! Thank you for your patience.
There is a lot of very useful content in this question base and your answers. Thanks to your team!
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Sun Mar 01, 2015 12:43 pm
by giorgiadiro
why this question has a different answer of question enthuware.ocajp.i.v7.2.837?
In 837 is
if(b instanceof Flyer) System.out.println("b is a Flyer");
very similar: b cannot be a Flyer as it cannot be a Bird.
But in this case the correct answer says that the program compiles.
what's the difference?
I found both the questions in the same simulation but I answered correctly only the enthuware.ocajp.i.v7.2.837 (no compilation errors).
can someone explain please?
Thanks
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Sun Mar 01, 2015 5:44 pm
by admin
The explanation to 837 and 838 explain exactly the question that you are asking. Did you read it? Please let me know which part is not clear so that we can provide more details.
Also, please go through the explanation of option 1 of 838.
The difference is that Bird is a class and Flyer is an interface.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Sun Mar 08, 2015 7:05 am
by giorgiadiro
admin wrote:
The difference is that Bird is a class and Flyer is an interface.
.
ok, this is the point and I missed it.
the explanation is clear.
Let me recap:
if(b instanceof Bird)
in question 838 results in a compilation error because b cannot reference an instance of a class that extends Bird.
it's not false, it's impossible because Bat doesn't extends Birds and in consequence no subclasses of Bat (that can be referenced by b) can.
if(b instanceof Flyer)
in question 837 results in a false statements because, even if Bat doesn't implements Flyer, a subclass of Bat (that can be referenced by b) could.
So in this case the statement is not impossible, it's only false at runtime.
Right?
Thank you.
(english is not my language, I hope I explain correctly)
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Sun Mar 08, 2015 12:32 pm
by admin
Yes, you got it

Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Wed Mar 08, 2017 8:17 pm
by dxie1154
I am a bit confused between the explanations on the forum and the one in the mock test.
In this forum, I understood that there was a compiler error because Bat can never pass the instanceof test, because it does not extend bird.
However, in the mock test, I understood that the object was pointing to an object of Bat, or a subclass of it. There would be a compiler error because it is impossible for the object to be a subclass of both Bat and Bird because you can't have two subclasses.
Which would be the proper explanation?
Re: About Question enthuware.ocajp.i.v7.2.838 :
Posted: Wed Mar 08, 2017 9:45 pm
by admin
You probably missed the last part of the explanation, "Therefore, it is not possible for b to point to an object of a class that "is a" Bird. The compiler figures out this fact at compile time itself and so the code fails to compile.".
This is what the discussion in this forum is also getting at. Since a class cannot be a subclass of two classes (Bat and Bird), it is not possible for b to pass the instanceof test.