• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference Conversions doubt

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the following question:

interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Orange {
public static void main(String args[]) {
Base base = new Base();
I1 i1 = base; // 1
Sub sub = (Sub)base; // 2
}}

Answer:Run-time error at line 2


Doubts:
1. why error @ runtime and not compile time?
2. is this always the case that whenever we downcast a parent class we will get runtime error?

Please help,

regards,
gitesh
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gitesh Ramchandani:
For the following question:

interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Orange {
public static void main(String args[]) {
Base base = new Base();
I1 i1 = base; // 1
Sub sub = (Sub)base; // 2
}}

Answer:Run-time error at line 2


Doubts:
1. why error @ runtime and not compile time?
2. is this always the case that whenever we downcast a parent class we will get runtime error?

Please help,

regards,
gitesh



Yes Gitesh downcasting is runtime exception.
[ August 27, 2007: Message edited by: Akhilesh Trivedi ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Your code compiles because the compiler sees that the base (reference ) can potentially have a Sub Object , so it allows your code to compile .
Now at runtime when you have a Base object referred by base (reference ) ,
which you are trying to cast to a Sub type , so your code gives the exception .

2. is this always the case that whenever we downcast a parent class we will get runtime error?

You cannot assign a Base Object to a reference of a Sub Type . never .

I think you need to understand what is a
1) Object ( not the java.lang.Object ).
2) Reference to an Object .
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for help.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic