About Question enthuware.ocajp.i.v7.2.872 :
Moderators: Site Manager, fjwalraven
-
- Posts: 32
- Joined: Wed Mar 14, 2012 5:45 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.872 :
this question is very ambiguous!!!
You're giving us options to choose from based on:
A a = null;
AA aa = null;
No where in the question says that not even a hint:
A = new AA();
i suggest you rephrase this question.
Thanks.
You're giving us options to choose from based on:
A a = null;
AA aa = null;
No where in the question says that not even a hint:
A = new AA();
i suggest you rephrase this question.
Thanks.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.872 :
There is no hint required to answer the question. The question is not ambiguous at all.
HTH,
Paul.
HTH,
Paul.
-
- Posts: 1
- Joined: Wed Mar 27, 2013 4:10 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.872 :
The question is not ambigous but maybe the explanation could be improved.
((AA)a).doStuff(); will compile as this code do :
String s = null;
s.toString();
and throw a NullPointerException. If the object a is instanciated then we will have a ClassCastException.
You did a very good job for most of the explanation, thank you.
((AA)a).doStuff(); will compile as this code do :
String s = null;
s.toString();
and throw a NullPointerException. If the object a is instanciated then we will have a ClassCastException.
You did a very good job for most of the explanation, thank you.
-
- Posts: 14
- Joined: Mon Nov 03, 2014 5:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.872 :
I had the answer correct, but I have a question above the explanation of the last option. This was the question:Last option: With the explanation: "Once you cast a to AA, you can call methods defined in AA. Of course, if a does not point to an object of class AA at runtime, a ClassCastException will be thrown."
My question is: In this particular case will this option throw a ClassCastException or a NullPointerException (since a is null)? My guess is a ClassCastException since a doesn't point to an object of class AA (but instead it points to null), but I'm not sure.
Code: Select all
class A{ public int getCode(){ return 2; }
class AA extends A{ public void doStuff(){ } }
// Given the following two declarations, which of the options will compile?
A a = null;
AA aa = null;
Code: Select all
((AA)a).doStuff();
My question is: In this particular case will this option throw a ClassCastException or a NullPointerException (since a is null)? My guess is a ClassCastException since a doesn't point to an object of class AA (but instead it points to null), but I'm not sure.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.872 :
In this particular case it will throw NPE because a null can be cast as any class. Since a points to null, it can be cast as AA. So no CCE.
HTH,
Paul.
HTH,
Paul.
-
- Posts: 14
- Joined: Mon Nov 03, 2014 5:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.872 :
Ah ok, thanks for the answer. Maybe that should be added to the answer. It isn't really relevant to the question itself, but it's still good to know that it will throw a NullPointerException in this case, instead of a ClassCastException.
Something like: "Once you cast a to AA, you can call methods defined in AA. Of course, if a does not point to an object of class AA at runtime, a ClassCastException will be thrown.
In this case however, a is null, so there won't be a problem with the casting (since an AA-object can also be null). Instead, it will throw a NullPointerException."
Something like: "Once you cast a to AA, you can call methods defined in AA. Of course, if a does not point to an object of class AA at runtime, a ClassCastException will be thrown.
In this case however, a is null, so there won't be a problem with the casting (since an AA-object can also be null). Instead, it will throw a NullPointerException."
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.872 :
Added.
Thank you for your feedback!
Thank you for your feedback!
Who is online
Users browsing this forum: No registered users and 8 guests