About Question enthuware.ocajp.i.v7.2.1244 :
Moderator: admin
Re: About Question enthuware.ocajp.i.v7.2.1244 :
The answer is right but the explanation is quite confusing. I do not get how line 2 will run fine but line 3 will throw runtime exception. Both a and a1 are of type A and refer to the same object.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
No, a and a1 are NOT referring to objects of type A.Michailangelo wrote:The answer is right but the explanation is quite confusing. I do not get how line 2 will run fine but line 3 will throw runtime exception. Both a and a1 are of type A and refer to the same object.
If you look at //1, a is actually referring to an object of type B[].
a1, otoh, is referring to an object of type A[], so at //3, when you try to cast it to b[], it will fail.
From your question, it seems it might be helpful to you if you read about the difference between the type of a variable and the type of the object pointed to by a variable. Once you understand this concept, casting will be a piece of cake for you
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Yeah, you are right. I completely missed this.
-
- Posts: 14
- Joined: Sat Mar 01, 2014 9:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
sorry but I just want to clarify a point:
1. Does it mean that a=b implicitly casts all B elements inside 'b' to A?
2. Does it mean that b = (B[]) a; explicitly cast back all elements inside a to B?
1. Does it mean that a=b implicitly casts all B elements inside 'b' to A?
2. Does it mean that b = (B[]) a; explicitly cast back all elements inside a to B?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
No, in case of arrays, a has to point to an array of B for b=(B[]) a to work. You can't "cast" an array of Objects (i.e. an Object[]) to a String[] even if all the elements of that Object[] are actually Strings.
I am not sure if you are aware of the fact that casting doesn't actually change the object. When you do X x = (X)y; The object pointed to by y doesn't "become" X, if it is not already an X. If y pointed to Y, then it will remain a Y.
You are just telling the compiler that the object pointed to by y is-a X and so all that is valid for X is valid for this object.
Now, when you do b = (B[]) a; it just means that you are telling the compiler that a points to an array of Bs. The elements of a don't change. They remain as they are. If a is not pointing to an array of B at runtime, then there will be an exception at run time.
I am not sure if you are aware of the fact that casting doesn't actually change the object. When you do X x = (X)y; The object pointed to by y doesn't "become" X, if it is not already an X. If y pointed to Y, then it will remain a Y.
You are just telling the compiler that the object pointed to by y is-a X and so all that is valid for X is valid for this object.
Now, when you do b = (B[]) a; it just means that you are telling the compiler that a points to an array of Bs. The elements of a don't change. They remain as they are. If a is not pointing to an array of B at runtime, then there will be an exception at run time.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Sat Feb 22, 2014 3:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Line 1 is not asked about in any of the options. Is that on purpose?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
While in this case it is referred in the explanation, you may find code or annotations in the code that have no bearing on the options.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun Apr 06, 2014 7:46 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
I don't know if I should start a new thread here or just post my question but....
Line 1 doesn't make sense to me. The explanation says its OK because 'assignment is done from a subclass reference to a superclass reference but I don't see inheritance relationship shown in the question. How can line 1's assignment happen without a cast?
Line 1 doesn't make sense to me. The explanation says its OK because 'assignment is done from a subclass reference to a superclass reference but I don't see inheritance relationship shown in the question. How can line 1's assignment happen without a cast?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
The last line of code listing is class B extends A{}
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Jun 05, 2014 8:58 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
The first answer seems fine but I'm a bit confused by the 2nd correct option 'E' which states "The cast at line 2 is needed".What does this exactly mean? For what it is needed looks like there is an ambiguity or i might be missing something.
I tried the same code by commenting out the line 2 but didn't get any compilation errors.(Same as when that line 2 is present)
and the ClassCastException thrown at line 3 remained same when i tried to run it.
Please enlighten me on this what does that statement say?
I tried the same code by commenting out the line 2 but didn't get any compilation errors.(Same as when that line 2 is present)
and the ClassCastException thrown at line 3 remained same when i tried to run it.
Please enlighten me on this what does that statement say?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
The cast at line 2 is needed means you need the cast to be able to assign a to b. i.e. b = a; will not work. You need to add the cast i.e. b = (B[]) a;
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Jun 05, 2014 8:58 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Oh yeah, i got it. I was seeing the whole statement every time i read cast at line 2 is needed.admin wrote:The cast at line 2 is needed means you need the cast to be able to assign a to b. i.e. b = a; will not work. You need to add the cast i.e. b = (B[]) a;
HTH,
Paul.
Thanks Paul.
-
- Posts: 5
- Joined: Thu Apr 02, 2015 4:28 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Hi,
looks like 4th answer is correct as well - "The program will compile and run if the (B[ ] ) cast in the line 2 and the whole line 3 is removed." If yes, how to determine in such questions what option should be selected?
looks like 4th answer is correct as well - "The program will compile and run if the (B[ ] ) cast in the line 2 and the whole line 3 is removed." If yes, how to determine in such questions what option should be selected?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Did you read the explanation? It explains exactly why the cast at line //2 is needed.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Thu Apr 02, 2015 4:28 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Yes, I read the explanation, it's clear, no questions here.
There was another question, - "if the cast at line //2" is correct statement from the first part of 4th answer, then the second part of 4th answer is "if the whole line 3 is removed" which is also looks like correct. Thus, 4th answer fit for this question as well as 3 and 5 or not?
There was another question, - "if the cast at line //2" is correct statement from the first part of 4th answer, then the second part of 4th answer is "if the whole line 3 is removed" which is also looks like correct. Thus, 4th answer fit for this question as well as 3 and 5 or not?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
May be I am not able to understand your question. Cast at //2 is required. You can't remove that cast. So "if the cast at line //2 is removed" is not correct. No matter what you do with //3.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Thu Apr 02, 2015 4:28 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Sorry for delay,
looks like I didn't understand correctly option 4, in particular, that "is removed" applied to both "the whole line 3" and "Cast at //2". I thought "is removed" applied to "the whole line 3" only and didn't to "Cast at //2".
looks like I didn't understand correctly option 4, in particular, that "is removed" applied to both "the whole line 3" and "Cast at //2". I thought "is removed" applied to "the whole line 3" only and didn't to "Cast at //2".
-
- Posts: 56
- Joined: Tue Feb 21, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Just to confirm, is it true that any super class (class that's higher in the hierarchy) can be cast to any subclass and pass compilation (with ClassCastException potentially at runtime)? Or are there cases where a super to subclass cast will throw a compile time error?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Yes, that is correct.lenalena wrote:Just to confirm, is it true that any super class (class that's higher in the hierarchy) can be cast to any subclass and pass compilation (with ClassCastException potentially at runtime)?
Nope. Never.Or are there cases where a super to subclass cast will throw a compile time error?
Just keep in mind that it is not the object that you cast. Objects don't change their types. They remain what they are. It is the reference variables that you cast. You change the type of a reference variable when you cast it to another type. For example, SubKlass sk = (Subclass) k; Here, you are temporarily changing the type of k from Klass to SubKlass and assigning it to sk. The actual object pointed to by k will remain whatever type it was before.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 56
- Joined: Tue Feb 21, 2017 4:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
It's clear now, thank you.admin wrote:Yes, that is correct.lenalena wrote:Just to confirm, is it true that any super class (class that's higher in the hierarchy) can be cast to any subclass and pass compilation (with ClassCastException potentially at runtime)?
Nope. Never.Or are there cases where a super to subclass cast will throw a compile time error?
Just keep in mind that it is not the object that you cast. Objects don't change their types. They remain what they are. It is the reference variables that you cast. You change the type of a reference variable when you cast it to another type. For example, SubKlass sk = (Subclass) k; Here, you are temporarily changing the type of k from Klass to SubKlass and assigning it to sk. The actual object pointed to by k will remain whatever type it was before.
HTH,
Paul.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Hi Admin!
I want to ask just one question:
a1=a; // the reference a1 now is pointing to the a object, right?
a=b;// the reference a now is pointing to the b object, right?
I would like to know after these to statements, why is not pointing a1 to the b object??
Why is there not conexion between a1 and b??
It is just because the references are pointing to objects and not to other references??
Thank you so much!!
I want to ask just one question:
a1=a; // the reference a1 now is pointing to the a object, right?
a=b;// the reference a now is pointing to the b object, right?
I would like to know after these to statements, why is not pointing a1 to the b object??
Why is there not conexion between a1 and b??
It is just because the references are pointing to objects and not to other references??
Thank you so much!!
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
You have a fundamental problem in your understanding. a, a1, and b are not objects. They are references pointing to objects. (References don't point to other references. A reference can only point to an object or nothing i.e. null).
It's like this - let's say you have a TV (call it, TV1) and a remote (call it, a) that is pointing to that TV. The TV is the object and the remote is a reference pointing to that object.
You now buy another remote (a1) and set this remote to point to the same TV as your previous remote by doing a1 = a. Now you have two remotes pointing to the same TV i.e. TV1.
Now, you change your first remote(a) to point to another TV (TV2) that is pointed to by some other remote b by doing a = b. So now, you have remote b as well as remote a pointing to TV2.
But what happens to the remote a1? Nothing. The second remote will still be pointing to the same TV1 that it was set to point to earlier. Setting a to point to another remote doesn't affect what b is pointing to.
It's like this - let's say you have a TV (call it, TV1) and a remote (call it, a) that is pointing to that TV. The TV is the object and the remote is a reference pointing to that object.
You now buy another remote (a1) and set this remote to point to the same TV as your previous remote by doing a1 = a. Now you have two remotes pointing to the same TV i.e. TV1.
Now, you change your first remote(a) to point to another TV (TV2) that is pointed to by some other remote b by doing a = b. So now, you have remote b as well as remote a pointing to TV2.
But what happens to the remote a1? Nothing. The second remote will still be pointing to the same TV1 that it was set to point to earlier. Setting a to point to another remote doesn't affect what b is pointing to.
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
A a = new B();it seems it might be helpful to you if you read about the difference between the type of a variable and the type of the object pointed to by a variable.
A - type of a variable
B - type of the object
Am i correct?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Correct.Sergey wrote:A a = new B();it seems it might be helpful to you if you read about the difference between the type of a variable and the type of the object pointed to by a variable.
A - type of a variable
B - type of the object
Am i correct?
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
a1 fails at run time since a1 hold an instance of A not B ? but a hold an instance of B therefore it is valid.
Who is online
Users browsing this forum: No registered users and 44 guests