Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

plz explain downcasting and upcasting  RSS feed

 
nagaraj raja
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai to all,
I know typecasting but i am eager to know what is downcasting and upcasting?plz explain
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java in General (Beginner) from SCJP
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These terms refer to casting an object reference (i.e. a variable, return value, etc.) to a subclass or superclass of its declared type. If you view an inheritence hierarchy as a tree with Object at the top and all subclasses of Object as its children and so on, then upcasting is when you have a reference to a child and cast it to a parent class or interface type. Downcasting is the reverse: converting a parent reference to a child. Upcasting is perfectly safe and often can be done automatically. However, downcasting is not completely safe since the compiler cannot know what the actual type of the object is.

Perhaps I can illustrate with some examples from the Collections Framework:

You will probably see this quite often when using Collections. Notice that on the right hand side, we create an ArrayList object with the keyword new. The result is a reference to this ArrayList object. But we are assigning it to a reference to List instead. The compiler will perform an implicit downcast here since ArrayList implements the List interface.

Prior to Java 1.5, all Collections could only store objects as Object references. (Generics has changed this, but that's another issue and beyond the scope of this discussion.) In such a situation, you often have to explicitly downcast an object when you get it out of the Collection. As an example, we will use the List l declared above:

Since List.get() is declared to return an Object, we must cast it to a String if there is actually a String at index 0 and we want to manipulate it as such. This is a classic exmaple of downcasting since the compiler cannot know what the actual type of the object that is returned by the get() method.

I hope this clarifies the differences between upcasting and downcasting. If you still have any questions, feel free to ask for clarifications.

Layne
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pls. correct if I'm wrong
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Megs Maquito:
pls. correct if I'm wrong


The terminology is slightly different when you are dealing with primitives. Because of some other differences between double and int, I will modify your example slightly:



The first conversion is called "widening" because a long has more bits than an int. The second is called "narrowing" because an int has less bits than a long. I don't think the terms "upcast" and "downcast" are commonly used in such a situation.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!