File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How can i let jvm to continue its process without giving runtime error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can i let jvm to continue its process without giving runtime error?" Watch "How can i let jvm to continue its process without giving runtime error?" New topic
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: 1296
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: 11160
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: How can i let jvm to continue its process without giving runtime error?
 
Similar Threads
Assigning base class object to sub class reference ?
overloading ...
Island of Isolation
Question about Sequence Diagram
Thoughts: Versioning, Class Reloading, ClassLoaders