+ 1
Upcasting and downcasting
Need of upcating and downcasting
3 Réponses
0
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 :)
+ 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.
0
But need for doing that is still unknown..