• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting  RSS feed

 
vendikonda sravan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some buddy Help me understand Casting in Java like what is up-casting/down-casting and what is legal what is not ?

Thanks in advance
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 198
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helps you in understanding Casting..

webpage
 
Geoff Jefferson
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Let me see if I can get this right.
An up cast is going from the specific towards the general.
For example your implementing a method in a class that is
further up the hierarchy. A down cast, usually described as
a 'cast' is when you convert an object that is below a class
to an object that is above in the hierarchy such as
beagle obj = (beagle) dog;

G.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An up-cast is safe; if you go from specific to general, you can be sure that a Beagle is a Dog.

A down-cast is not safe; you can have a Dog but have no confidence that it is a Beagle. You will suffer a ClassCastException if you are mistaken about that.

And please search the Ranch; this sort of question comes up every few weeks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!