+ 1

Explain this upcasting!!!

why Animal a = new Cat(); instead of Cat a = new Cat(); What is the difference? Why do we do the former?

30th Aug 2018, 7:58 AM
Divyani Singh
Divyani Singh - avatar
1 Odpowiedź
+ 5
It depends on what you plan on doing. Making a Cat it's parent type allows for code that doesn't need to know whether it is processing a Cat or Dog to process either. If you declared it as Cat, the code would have to be written twice (both versions for Cat and Dog.)
30th Aug 2018, 11:40 AM
John Wells
John Wells - avatar