Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Referene Variable Casting

 
George Gates
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a doubt in reference variable casting.

Assume Dog extends Animal.

1) Is it true that the second statement will compile but not run:


In other words is it safe to believe that, whenever we are down-casting, the program will always compile but never run?

2)

For up-casting is it safe to believe that, it will always compile and run fine.
Are there any special cases where up-casting can cause compilation or run problems?

Thanks

Regards,
Badal Chowdhary
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried running the statement?
 
Ben Smither
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2) true
1) when Animal a = new Dog(); it runs.



Originally posted by Badal Chowdhary:
Hi All,

I have a doubt in reference variable casting.

Assume Dog extends Animal.

1) Is it true that the second statement will compile but not run:


In other words is it safe to believe that, whenever we are down-casting, the program will always compile but never run?

2)

For up-casting is it safe to believe that, it will always compile and run fine.
Are there any special cases where up-casting can cause compilation or run problems?

Thanks

Regards,
Badal Chowdhary
 
Paul Prusko
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can think of cases where down-casting does not cause any runtime exceptions like the one below:
( suppose class Dog extends Animal )

so this is not a rule that down-casting ends up with a runtime problem.
Regards,
Paul.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic