• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cast question

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why line2 outputs "Animal" since reference a is casted to type Dog and line3 outputs "Dog" by recast a to type Dog?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The field value is resolved with the declared type of the field, i.e. Animal.
The statement:
a = (Dog)a;
does not change the declared type of a. The reference variable a will always be of (declared) type Animal and you will not be able to change that. The only way to access the instance variable s in class Dog is to do it as done on line 3.

For more information, please check out JLS 8.3.3.2 Example: Hiding of Instance Variables
[ February 13, 2003: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin, I know this is extremely off-topic and I apologize for it in advance, but can I ask you from where did you get your quote? because it seems familiar, but I can�t quite place it.
thanks,
Francisco
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[hijack]
Valentin, I know this is extremely off-topic and I apologize for it in advance, but can I ask you from where did you get your quote? because it seems familiar, but I can�t quite place it.
I'm quite sure you have seen that movie (who hasn't?)... The Matrix!!
[/hijack]
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin, thanks a lot.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a=(Dog)a;
System.out.println(a.s); //line2: "Animal"


this equals to the following:

Animal youWantToReferenceItAsDogType=new Dog();
System.out.println(youWantToReferenceItAsDogType.s); //line2: "Animal"

so the type of youWantToReferenceItAsDogType is also Animal,and then the output is also "Animal"!
[ February 15, 2003: Message edited by: Mellihoney Michael ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic