• 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

Runtime Exception

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

The above code show runtime exception as below

Exception in thread "main" java.lang.ClassCastException
at CEx.main(CEx.java:10)



could any one explain whats wrong in this code?
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At runtime the object is tested for the type casted.
The Sub class "is a" base class and the reverse is not true.
Hence u get the error.
It compiles fine as only the cast type(not the true object)is checked at compile time.
Hope this helps...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have replied for some similar kind of question in another topic.

class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class Mobile
{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b; //Line no 10


}
}

Yes! its a Runtime error, coz of not following hierarchy reflection. you can't assign the obj of base class to the derived class object. But u can assign the obj of derived class to the base class object.

Example code for ur prg: No Compiler or runtime ERROR.

Sub _sub = new Sub();
b = _sub;
Sub2 _sub2 = new Sub2();
b = _sub2;


The point is Derived class may have some additional functionality of Base class, So u cant assign the base class object to derived class object.

The compiler would have shown the error if you had not type cast the b to (Sub). the copiler only checks the r.side operand sink with l.side operand in the assignment operator.

This cast incompatability will come to know only at the runtime.

with regards,
Chiyan
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I make one simple change in your code. Its now work fine.
I don't know whats done inside of the jvm. Could any one explain me
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

b now points to another object which is legal. Though b of type Base it is referring to a new object which is its subclass so it is legal.
no when you apply a cast to b

you are doing a type cast on the reference object which is of type Sub, so it is legal.
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Why it is not valid ?
reply
    Bookmark Topic Watch Topic
  • New Topic