About Question enthuware.ocajp.i.v7.2.838 :
Moderator: admin
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.838 :
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.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.
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.
-
- Posts: 18
- Joined: Fri May 31, 2013 1:18 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
A bit longer explanation that might help someone:
http://www.coderanch.com/t/481356/java- ... ompilation
http://www.coderanch.com/t/481356/java- ... ompilation
-
- Posts: 22
- Joined: Wed Jan 08, 2014 11:24 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.
-
- Posts: 15
- Joined: Sun Jun 01, 2014 8:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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?
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");
}
}
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.
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.
-
- Posts: 15
- Joined: Tue Jan 13, 2015 12:10 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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!
There is a lot of very useful content in this question base and your answers. Thanks to your team!
-
- Posts: 7
- Joined: Wed Feb 04, 2015 1:06 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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
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
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.
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.
-
- Posts: 7
- Joined: Wed Feb 04, 2015 1:06 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
ok, this is the point and I missed it.admin wrote: The difference is that Bird is a class and Flyer is an interface.
.
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)
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
Yes, you got it 

-
- Posts: 9
- Joined: Sat Jan 14, 2017 5:01 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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?
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?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.
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.
Who is online
Users browsing this forum: No registered users and 8 guests