Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Aviance School is one of the largest web solutions platform in India for developers to learn and share their programming knowledge and build their careers.
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