+ 1

Upcasting and downcasting

Need of upcating and downcasting

10th Jun 2020, 7:32 AM
kotresh v.g
kotresh v.g - avatar
3 Answers
Say you write a function that takes all elements in a list and sums them. But, Java has so many different lists and collections: ArrayList, List, Set, BlockingQueue, HashSet, Stack, Vector, LinkedList, .... Do we have to write a new function for all of these types, that does exactly the same? No, they all inherit from `Collection`—so you write your function for the type `Collection` and now you can use it for any list type by upcasting. I suggest you keep coding, it will all become clear in time :)
10th Jun 2020, 7:52 AM
Schindlabua - avatar
+ 2
Upcasting (Generalization or Widening) is casting to a parent type in simple words casting individual type to one common type is called upcasting while downcasting (specialization or narrowing) is casting to a child type or casting common type to individual type.
10th Jun 2020, 7:35 AM
👑FabVab👑 - avatar
But need for doing that is still unknown..
10th Jun 2020, 7:36 AM
kotresh v.g
kotresh v.g - avatar