What is Downcasting?
What is Downcasting?
what is downcasting in java | upcasting and downcasting in java javatpoint | upcasting and downcasting in java geeksforgeeks | downcasting c++ | upcasting and downcasting in c# | what is the use of upcasting and downcasting in java | downcasting meaning | java downcasting best practices
what is downcasting in java | upcasting and downcasting in java javatpoint | upcasting and downcasting in java geeksforgeeks | downcasting c++ | upcasting and downcasting in c# | what is the use of upcasting and downcasting in java | downcasting meaning | java downcasting best practices
what is downcasting in java | upcasting and downcasting in java javatpoint | upcasting and downcasting in java geeksforgeeks | downcasting c++ | upcasting and downcasting in c# | what is the use of upcasting and downcasting in java | downcasting meaning | java downcasting best practices
what is downcasting in java | upcasting and downcasting in java javatpoint | upcasting and downcasting in java geeksforgeeks | downcasting c++ | upcasting and downcasting in c# | what is the use of upcasting and downcasting in java | downcasting meaning | java downcasting best practices
what is downcasting in java | upcasting and downcasting in java javatpoint | upcasting and downcasting in java geeksforgeeks | downcasting c++ | upcasting and downcasting in c# | what is the use of upcasting and downcasting in java | downcasting meaning | java downcasting best practices
what is downcasting in java | upcasting and downcasting in java javatpoint | upcasting and downcasting in java geeksforgeeks | downcasting c++ | upcasting and downcasting in c# | what is the use of upcasting and downcasting in java | downcasting meaning | java downcasting best practices what is downcasting in java upcasting and downcasting in java javatpoint upcasting and downcasting in java geeksforgeeks downcasting c++ upcasting and downcasting in c# what is the use of upcasting and downcasting in java downcasting meaning java downcasting best practices
Share
Downcasting with java instance of operator. When we want to cast a Super class to Sub class, we use Downcasting and it is not directly possible in Java.
Cat c = new Cat();
Animal a = (Animal) c; //Explicitly you have done upcasting. Actually no need, we can directly type cast like Animal a = c; compiler now treat Cat as Animal but still it is Cat even after upcasting
c.callme();
a.callme(); // It calls Cat's method even though we use Animal reference.
((Cat) a).callme2(); // Downcasting: Compiler does know Animal it is, In order to use Cat methods, we have to do typecast explicitly.
// Internally if it is not a Cat object it throws ClassCastException