About Question enthuware.ocajp.i.v7.2.1244 :
Moderator: admin
-
- 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: 10065
- 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: 10065
- 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.
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
can it be corrected like this? (I try it and run)
The line //1 will be allowed during compilation, since assignment is done from a subclass reference to a superclass reference.
The line //1 will be allowed during compilation and run time, since assignment is done from a subclass reference to a superclass reference.
The line //1 will be allowed during compilation, since assignment is done from a subclass reference to a superclass reference.
The line //1 will be allowed during compilation and run time, since assignment is done from a subclass reference to a superclass reference.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1244 :
Yes, it will work even at run time.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 106 guests