| Author |
How can i let jvm to continue its process without giving runtime error?
|
arulraj michealraj
Greenhorn
Joined: Mar 02, 2005
Posts: 28
|
|
I have one doubt in following code class A { } class B extends A { } 1. B objB=new A();// it will say compile time error. In order to avoid it, we can change that as B objB=(B) new A(); Even though we have changed it, it will say run time error. My question is that How can i let jvm to continue its process without giving runtime error for the above problem?
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
You can't. For the same reason that the following wont work: String o = (String) new Object(); or ArrayList l = (ArrayList) new Object();
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Rajasekar Elango
Ranch Hand
Joined: Sep 13, 2004
Posts: 105
|
|
You could enclose your code in try block and simply ignore the exception in catch block.. But catching ignoring exception is generally a bad practice as it conceals the problem.. -- Raja
|
SCJP 1.4
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
Remember how things work. when you say B objB you are saying "the thing that objB points to is a B object. It can do anything a B object can do." you stick an A object in there. you call a method that is defined in B (which you can do, because the reference is what determines the methods to call) - what should happen? there is no method defined for that object with that name. What i don't understand is why you would want to do this?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: How can i let jvm to continue its process without giving runtime error?
|
|
|