+ 2
can anyone give examples for downcasting here ?
2 ответов
- 1
class Cat extends Animal {}
class MyClass {
public static void main (String [] args){
Cat myCat = new Cat ();
Animal animal = (Animal) myCat;
//is it upcasting? i don't remember. lol
//if the above example is upcasting
Animal animal = new Animal ();
Cat myCat = (Cat) animal;
}
}
- 1
for downcasting is require to do casting explicitly but for upcaating is not necessary to do explicitly jvm will automatically do it implicitly.
int price = 22 ;
double value = price ;// upcasting. is not necessary explicit
double value = 32.45 ;
int price = (int) value // downcasting