About Question enthuware.ocajp.i.v7.2.838 :
Moderators: Site Manager, fjwalraven
About Question enthuware.ocajp.i.v7.2.838 :
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
Reason: Updated question id to 838
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.837 :
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.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.837 :
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."
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.837 :
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.
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.
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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?
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?
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
"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.
HTH,
Paul.
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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?
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?
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.
"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.
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.
So I understand that instanceof is not for lazy programmers, you have to figure out yourself if lho is an instanceof the rho...
Thanks.
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.

-Paul.
-
- Posts: 10
- Joined: Sat Jan 24, 2015 8:09 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
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.
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.
-
- Site Admin
- Posts: 10389
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.838 :
Sure, updated.
thank you for your feedback!
thank you for your feedback!
Who is online
Users browsing this forum: No registered users and 1 guest