• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can i let jvm to continue its process without giving runtime error?

 
arulraj michealraj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. For the same reason that the following wont work:

String o = (String) new Object();

or

ArrayList l = (ArrayList) new Object();
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic