Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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

 
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?
 
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();
 
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
 
lowercase baba
Posts: 12915
64
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?
 
Arthur, where are your pants? Check under this tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic