Page 1 of 1

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

Posted: Tue Dec 04, 2012 12:59 am
by ETS User
The Objective-wise test "Working with Inheritance", question 14, indicates that this code will not compile. The explanation here states it will compile. So there are two different answers to the same question.

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

Posted: Tue Dec 04, 2012 6:04 am
by admin
I just checked this question and the option "It will not compile" is marked as a wrong option. So I don't see any issue. Can you please confirm?

HTH,
Paul.

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

Posted: Tue Dec 04, 2012 7:14 pm
by ETS user
Question 14 (enthuware.ocajp.i.v.7.2.838) in the Objective-wise tests for "Working with Inheritance" highlights "It will not compile" as the correct answer and states: "b points to an object of class Bat, which does not extend from Bird. Now, it is possible for b to point to an object of any subclass of Bat. However, it is not possible for that sub class to extend Bird (because a class can at most extend from only one class). Therefore, it is not possible for be to point to an object of a class that extends Bird. The compiler figures out this fact at compile time itself and so the code fails to to compile."

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

Posted: Tue Dec 04, 2012 8:22 pm
by admin
Hi,
I am sorry but I am not sure if I understand your doubt here. In the title of the topic, you mentioned 2.837, so I checked that, which is fine.

Now, you mentioned 2.838. For this question, "It will not compile" is the correct option. There is a detailed explanation as well about why it will not compile. There is additional explanation provided that explains why "b instanceof Flyer" (which is different from the options) will compile. This is provided to make sure that the student understands what is going on.

HTH,
Paul.

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

Posted: Wed Nov 13, 2013 4:56 pm
by javaman
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");
}
}

I figured "a instanceof b" returns "true" if b is somewhere up in the inheritance tree of a.
If it is not (as is the case with the line "b instanceof Bird") why doesn't this line just compile and return false when run?? What's the use of instanceof if it can not assert?

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

Posted: Wed Nov 13, 2013 5:03 pm
by admin
"a instanceof b" doesn't compile because the second operand to instanceof requires the name of a Java class and not a variable. This is a java language rule.

HTH,
Paul.

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

Posted: Wed Nov 13, 2013 5:39 pm
by javaman
in the line
if(b instanceof Bird) System.out.println("f is a Bird");
isn't "Bird" the name of a class and _not_ the name of a variable? So it should rfeturn flase instead of not compiling...
What am I missing?

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

Posted: Wed Nov 13, 2013 5:59 pm
by admin
In your post above you wrote "a instanceof b".

"b instanceof Bird" fails to compile because the compiler knows that b can never point to any object that is-a Bird. So no point in waiting till runtime.

Further details are given here: http://docs.oracle.com/javase/specs/jls ... ls-15.20.2

HTH,
Paul.

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

Posted: Wed Nov 13, 2013 6:14 pm
by javaman
ok thanks. Yes, sorry for confusing example with a and b...
So I understand that instanceof is not for lazy programmers, you have to figure out yourself if lho is an instanceof the rho...
Thanks.

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

Posted: Wed Nov 13, 2013 6:42 pm
by admin
No, that is not how you should see it. The compiler is helping the lazy programmer by telling him in advance that b instanceof Bird is impossible by raising a compilation error :) It is preventing the lazy programmer from putting a non-sensical line of code in the class that is sure to fail at run time.

-Paul.

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

Posted: Wed Mar 11, 2015 8:51 am
by EelcoD
Thanks for this topic.

Paul, maybe it's a good idea to provide a better explanation to why the code fails to compile, because I didn't understand the given explanation.

In the explanation you say: "Therefore, it is not possible for b to point to an object of a class that extends Bird. The compiler figures out this fact as compile time."

That explanation doesn't make sense to me, because b doesn't point to an object of a class that extends Bird, so that cannot be the reason why it fails.

I understood it after reading: ""b instanceof Bird" fails to compile because the compiler knows that b can never point to any object that is-a Bird."

That's a lot clearer .. or at least to me.

Thnx.

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

Posted: Wed Mar 11, 2015 9:26 am
by admin
Sure, updated.
thank you for your feedback!