Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting ...

 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends...
Can someone tell me why the following code gives Runtime Error at Commented line 1.
I want to know the logic behind this error.
class Light { /* ... */ }
class LightBulb extends Light { /* ... */ }
public class WhoAmI
{
public static void main(String args[])
{
LightBulb lightbulb = new LightBulb();
Light lig = new Light();
lightbulb = (LightBulb) lig; // Line 1
System.out.println("It Works... ");
}
}
Thanks in advance
 
Sri Yamujala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shah,
Actually, It compiled fine. It didn't give any error.
If you remove the casting, it would give you an error for incompatible class types.
Sri
Originally posted by Shah Chunky:
Hi Friends...
Can someone tell me why the following code gives Runtime Error at Commented line 1.
I want to know the logic behind this error.
class Light { /* ... */ }
class LightBulb extends Light { /* ... */ }
public class WhoAmI
{
public static void main(String args[])
{
LightBulb lightbulb = new LightBulb();
Light lig = new Light();
lightbulb = (LightBulb) lig; // Line 1
System.out.println("It Works... ");
}
}
Thanks in advance

 
Subramaniam Venkatesan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shah,
runtime error because of down-casting...


Thanks in advance[/b]
</BLOCKQUOTE>

[This message has been edited by Subramaniam Venkatesan (edited February 27, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic