+ 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!
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
+ 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 !!
0
Can u explain real life example of upcasting and downcasting