• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Reference Conversion

 
Guy A. Buyle
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mayuri Lakhani
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alfred,
Your above posting was very understandable. Waiting for ur next posting for conversion rules of interface <==> class.
Mayuri
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic