This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reference variable assignment problem

 
ujjawal rohra
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does line 9 gives an error when we have referred a to b in line 7???
 
Rajeev Rnair
Ranch Hand
Posts: 308
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Fred David
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujjawal rohra wrote:Again a slight variation of the above code is




 
ujjawal rohra
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesnt line 2 mean that a is now referring to a B type object..???

Correct me if i m wrong
 
Rajeev Rnair
Ranch Hand
Posts: 308
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 774
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
ujjawal rohra
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes rajiv thats clear now i was just confused with the addresses
 
Rajeev Rnair
Ranch Hand
Posts: 308
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujjawal rohra wrote:yes rajiv thats clear now i was just confused with the addresses

good luck friend with preparations
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic