Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriden method throwing Exception.

 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,


If we run it with cmd arguemnt 0 it will print 0. We are sure that in the above code the overridden getInt() method(Subclass version) is calling. Then why we declare main method throws exception,when subclass getInt() method not throwing any exception. Please anyone explain?

Thanks in advance.
 
Jesse Custer
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Decision about wich overridden method will be called is made at runtime. The compiler doesn't know that the variable of type Utils actually contains a Ping. So we are forced to handle the exception or declare it like done here.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

yes, when you say
Ping u1 = new Ping() the code compiles without the "throws".

What's a bit confusing about the code, is that you are refering on the arguments of main. It would be easier to understand when you just say


Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

and now for something completely different.

Try to post what is here in code tags without code tags to the java ranch:

Ping you,
Bu.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot divulge that secret.
[ April 17, 2007: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic