Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting ..." Watch "Casting ..." New topic
Author

Casting ...

Shah Chunky
Ranch Hand

Joined: Dec 27, 2000
Posts: 116
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


Shah Chunky - Sun Certified Java2 Programmer.
Sri Yamujala
Greenhorn

Joined: Dec 07, 2000
Posts: 18
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


Sri
Subramaniam Venkatesan
Ranch Hand

Joined: Feb 24, 2001
Posts: 48
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).]
 
 
subject: Casting ...