File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes UpCasting DownCasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "UpCasting DownCasting" Watch "UpCasting DownCasting" New topic

UpCasting DownCasting

Thirukumaran Gopalakrishnan

Joined: Aug 11, 2006
Posts: 2
Upcasting DownCasting example please.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59

ArrayList a = new ArrayList();

Collection c = a; // upcast implicit no need for (ArrayList)a

ArrayList b = (ArrayList)c; // downcast requires explicit cast

upcast dont require an explicit cast as a subclass is always guaranteed to pass the is-a test with its parent. ie ArrayList is a Collection.

downcast requires an explicit cast because the object being cast might not be what its being cast to. You are basically saying to the compiler trust me i know what im doing.

I agree. Here's the link:
subject: UpCasting DownCasting
It's not a secret anymore!