*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reference variable assignment problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "reference variable assignment problem" Watch "reference variable assignment problem" New topic
Author

reference variable assignment problem

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105


Why does line 9 gives an error when we have referred a to b in line 7???


SCJP 6
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

ujjawal rohra wrote:


B is a sub class of A
B is a A
but A is NOT a B

so you cannot cast an "A" object to a reference of "B" directly


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

here c IS-A B
and b IS-A A
hence we can assign b to a
but in the statement

B c = a;
we are assigning a to B that is not possible because a does not pass IS-A relationship
we can give B c = (B)a


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Fred David
Greenhorn

Joined: Apr 15, 2010
Posts: 5
With the explicit cast B c = (B) a; there is no ClassCastException because variable a is assigned to b on line 7 and b refers to an object that IS-A B. The cast is needed at compile time because variable a is declared as type A . With the explicit cast B c = (B) a; it compiles fine and all three variables refer to the same object, the one created on line 6.
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
but the code fails even when we declare

A a=new B();

and why the existing code is not working when we wrote the line

a=b;
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Again a slight variation of the above code is


doesnt work, when a is referring to a B???
Please help i cant understandd
Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 96

ujjawal rohra wrote:Again a slight variation of the above code is





SCJP6 | http://programmerutilities.blogspot.com/
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Doesnt line 2 mean that a is now referring to a B type object..???

Correct me if i m wrong
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

ujjawal rohra wrote:Doesnt line 2 mean that a is now referring to a B type object..???

Correct me if i m wrong


a (reference of "A") is pointing to a "B" object
the point is it is still a "A" reference, and that is why you needed an explicit cast !


Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

ujjawal rohra wrote:but the code fails even when we declare

A a=new B();

and why the existing code is not working when we wrote the line

a=b;

how did you assume that A a = new B() fails? There is nothing wrong here. Check the following code



This compiles and runs fine, hope this is clear
Please read through the K&B book on casting (both explicit and implicit), do some examples yourself and follow up the questions in the chapter.
These are very basics in casting, you need this to be very clear without proceeding further.


Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Dear Ujjwal,

Please understand this point that, what you are trying to achieve is a polymorphic assignment. The run-time
object will be B but reference is still A. A is a super-class of B. You can assign a sub-class reference to a
super-class reference, as an implicit cast will happen. Why? It is because you are restricting the operations
which can be performed which is quiet safe. But how can a super-class reference be assigned to a sub-class
reference, it won't pass IS-A test. That's why you get an error.

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Yes it helps but please make one point clear

Doesnt a=b make 'a' refer to a 'b' and doesnt it make 'a' point to a B..


What i read was that ''a=b'' will copy the bits in 'b' into 'a' that means 'a' will now have the address of 'b'..
and that 'a' will refer to a B..

Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

ujjawal rohra wrote:Yes it helps but please make one point clear

Doesnt a=b make 'a' refer to a 'b' and doesnt it make 'a' point to a B..


What i read was that ''a=b'' will copy the bits in 'b' into 'a' that means 'a' will now have the address of 'b'..
and that 'a' will refer to a B..



When you say


the reference is still "A" , even though "a" is pointing to a "B" object!
"a" will have the address of object "b", but still it is an "A" reference pointing to a "B" object.
This is a polymorphic assignment
The difference is that you cannot invoke any B methods (which A doesnt have) on "a" even though it points to a "B" object

For example




Hope this is clear!


ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
yes rajiv thats clear now i was just confused with the addresses
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

ujjawal rohra wrote:yes rajiv thats clear now i was just confused with the addresses

good luck friend with preparations
 
wood burning stoves
 
subject: reference variable assignment problem
 
Similar Threads
book K&B self test q16........problem
Question on therad q.16 pg755
Inner Classes (1)
K&B CHAPTER:8 q.no:9.........
clone() used on array and object