I did not understand even a little bit of this topic (Upcasting and downcasting). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I did not understand even a little bit of this topic (Upcasting and downcasting).

Please help me in understanding this topic

21st Apr 2018, 3:03 PM
Sri Vaibhav Mohan Dev
Sri Vaibhav Mohan Dev - avatar
2 Answers
+ 8
The upcasting is casting from the child class to base class. The upcasting in java is implicit which means that you don't have to put the braces(type) as a indication for casting. Below is an example of upcasting where we create a new instance from class C and pass it to a reference of type A. Then we call the function display. java codeThe downcasting is the casting from base class to child class. Below we continue on the previous upcasting snippet by adding to lines for downcasting where we down cast the aRef reference from type A to type B. Downcasting is explicit note the usage of braces(type) in the example below. java code
21st Apr 2018, 3:44 PM
Baraa AB
Baraa AB - avatar
21st Apr 2018, 3:19 PM
NezhnyjVampir