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

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ETS User

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

Post 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.
Last edited by admin on Thu Dec 06, 2012 6:36 am, edited 1 time in total.
Reason: Updated question id to 838

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

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

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

ETS user

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

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

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

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

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

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

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

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

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

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

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

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

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

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

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

javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

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

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

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

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

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

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

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

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

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

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

Post by admin »

Sure, updated.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests