This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting problem  RSS feed

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]Corrected code tags. CR[/edit]
[ September 12, 2008: Message edited by: Campbell Ritchie ]
 
Marshal
Posts: 59110
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the buttons for tags, much quicker than writing them by hand, and more effective.

In the first case you are declaring your object as an Ab; there is no z field in Ab. When you have a new declaration as a Bc, then it is a class which does have a z field.

When you declare a variable of a particular type, the compiler can only find the members of the declared type. At runtime the versions of the actual type are used, which is polymorphism and works for overridden members (ie instance methods) only. A classcast like that is dangerous; if you get it wrong you can have your application stop with Exceptions.
 
Author
Ranch Hand
Posts: 3418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:

 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this has to with operator precedence..

You are trying to access member z of type a and then cast that to type Bc with this piece of code.. (if this worked you would have cast an int to a class )
System.out.println((Bc) a.z);

With this piece of code you are casting an object type a to type Bc and then accessing the member z of type Bc
System.out.println(((Bc) a).z);
[ September 12, 2008: Message edited by: Gamini Sirisena ]
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys!!!
 
Sheriff
Posts: 21330
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Gamini Sirisena:
I think this has to with operator precedence..


Correct; . is at the top of the operator precedence list. So it will always evaluate . (and [] / ()) first, and only then will it cast. So it was trying to cast a.z to Bc.
 
Campbell Ritchie
Marshal
Posts: 59110
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot about the precedence problem
 
Paper beats rock. Scissors beats tiny ad.
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!