Google+

209. Downcasting







While Upcasting (i.e. Assigning Sub Class Object reference to Super Class Object reference) can be automatically done by Java. But Downcasting (i.e. Assigning Super Class Object reference to Sub Class Object reference) cannot be done automatically by Java, but we need to do it manually by following the below syntax -

Assuming that Animal Class is the super Class of the Cat Class -

Cat c1 = new Cat( );  //Creating Object for Cat Class
Animal a = c1; //Automatic Upcasting to Animal
Cat c2 = (Cat) a;  //manually Downcasting back to a Cat

Why Upcasting is automatical and Downcasting need to be done manually ?

Converting a Cat or Dog to Animal can never fail, hence the Upcasting is done automatically. But if you have a group of different animals and cast them all to a Cat, then there are some chances that some of the animals are actually dogs and the process fails. Hence Downcasting need to be done manually to avoid this problem.

After Downcasting, can you run the program sucessfully ?

The answer is Yes and No. The answer is Yes, when you just define the Super Class 'Animal' object instead of creating it. The answer is No, when you create Super Class 'Animal' object. Lets see the below examples to find out the cases -

Example1 - Yes Case - This Program can be Run without any compiler and run time errors. i.e. Dowcasting statement wont throw any errors.

Animal a = null;  //Object a is just define as Animal Class type here but not created.
Cat c2 = (Cat) a;  //Downcasting. This statement wont throw any errors.

Example2 - No Case - This Program wont throw compiler error but throws run time error. i.e. Downcasting statement throws run time error.

Animal a = new Animal( ); //Object created for Animal Class
Cat c2 = (Cat)a; //Downcasting. This statement wont throw compile time error but throws run time error.

Lets implement the Downcasting Objects on Eclipse IDE and find out whether we get any errors by following the below steps -

1. Launch Eclipse IDE, create a new Java Project 'Project 37' as shown below -


2. Create a new Java Class 'ClassOne' with main( ) method as shown below -



3. Create the Classes 'Animal' and 'Cat' Classes in the same 'ClassOne.java' Class file as shown below -


4. Make the 'Cat' Class as the Sub Class of 'Animal' Class as shown below -


5. Now create an object for Animal Class as shown below -


6. Manually Downcast the created Animal object to Cat Class object as shown below -


7. Run the Java Class file 'ClassOne.java' and observe that the Run time error is displayed as Output in the Console tab as shown below -


Though you have not got any compile time error, but when you have run the program, the above run time error is displayed.

8. Now lets resolve this run time error by modifying the Animal Super Class object creation statement to just defining statement as shown below -


9. View the error displayed in the Manually Downcasting statement as shown below -


As we are using the object in the manual downcasting statement without initializing it, we got this error.

10. Select the 'Initialize variable' option in the above error message to the compiler error as shown below -


11. Observe that the error got resolved and the null reference got assigned to the object as shown below -


Observe by following the above few steps (8-11) we have defined the Animal Super Class object instead of creating it and using the object in the manual downcasting statement.

12. Run the 'ClassOne.java' class file and observe that this time , no run time errors are displayed as shown below -


So the run time error displayed in the step 7 is not displayed after changing the object creating to object defining statement.

13. Till now we have downcasted the super class object manually, but lets findout what happens when you don't manually downcast the assigned super class object as shown below -


14. View the error displayed after removing the manual casting (Cat) as shown below -


15. Select the first option 'Add cast to 'Cat' ' in the error message as shown below -


16. Observe that manual casting (Cat) got added before the assigning super class object 'a' as shown below and the error got resolved as shown below -


Hence Automatic casting is not done when you Downcast. You have to manually cast while Downcasting.







Please comment below to feedback or ask questions.

instanceof operator will be explained in the next post






1 comment:

Martin said...

I got the o/p same as the step 12 but when I call a method with ob then it give a error like
Exception in thread "main" java.lang.NullPointerException
Why?

Followers :)

Blog Index