+ 4

Will someone explain me what is the difference between Casting and Upcasting/Downcasting in Java ?

I understand from Casting is about converting variables like from decimal to int or vise versa but this upcasting and downcasting thing is not clear for me. Can you give real life example please cause that Animal => Cat <> Dog example is not meaning to me!

8th May 2017, 6:49 AM
Melih Melik Sonmez
Melih Melik Sonmez - avatar
3 Respostas
+ 16
A type conversion from sub type to super type is up casting. In contrast, a type conversion from super type to sub type is down casting. Say, Fruit is a super class and Orange is its subclass. Then, Fruit f1 = new Orange(); // up casting Fruit f2 = new Fruit(); Orange o1 = (Orange) f2; // down casting
8th May 2017, 7:45 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 3
@Shamima Yasmin i clearly understand your code but i need real life example cause still i don't understand why will i need this up/down casting thing; sorry for being so noob but could not solve in mind !!
8th May 2017, 10:36 AM
Melih Melik Sonmez
Melih Melik Sonmez - avatar
0
Can u explain real life example of upcasting and downcasting
16th Apr 2018, 4:21 AM
Reena thakur
Reena thakur - avatar