File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Reference Conversion 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 "Object Reference Conversion" Watch "Object Reference Conversion" New topic
Author

Object Reference Conversion

Guy A. Buyle
Greenhorn

Joined: Nov 07, 2002
Posts: 14
I have tried to understand the way Object Reference convertion works. Do someone have a easy rule to remember when conversion is oke at compile time and when it is oke at run time.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Conversions of object references are summarized in the following points putting in mind I'm talking about classes<==>classes and NOT interfaces<==>classes:
- During compile time
1- Upcasting - meaning assigning references of type subclasses to a reference of type superclass - is always acceptable at compile time, even without an explicit casting.
2- Downcasting - meaning assigning references of type superclass to a reference of type subclass - NEEDS an explicit casting.
3- Conversion - even with explicit casting - of peer classes references - meaning there is no is-a relation between them - will always cause a compile time error.
- During runtime
4- Upcasting - meaning assigning references of Object RUNTIME type subclasses to references of superclasses - is always acceptable during runtime.
5- Downcasting - meaning assigning references of object RUNTIME type superclasses to references of subclasses - will throw a ClassCastException during runtime.
6- Of course peer casting doesn't actually pass the compile time to the runtime.
* Not that Reference Type can be different from Object Runtime Type.
With Interfaces <==> classes it's a different story, I may tell in a later post, but now i have to work
Please correct me if I wrote anything wrong
Hope that was clear.
[ November 21, 2002: Message edited by: Alfred Kemety ]

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Mayuri Lakhani
Greenhorn

Joined: Nov 18, 2002
Posts: 28
Hi Alfred,
Your above posting was very understandable. Waiting for ur next posting for conversion rules of interface <==> class.
Mayuri
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Well I'm sorry I don't have much time now, but if you read this post, you will have a very good understanding about interface <==> class conversion. It's a bit confusing though, but read the JLS quotes...
http://www.coderanch.com/t/239587/java-programmer-SCJP/certification/Aren-Collection-AbstractMap-peers-common
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Reference Conversion