• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

doubt in this code please help

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will be the result of compiling the following code:


the options are
1.overridden:hello(string,Integer[])
2.hello(string,Integer[])
3.compile time error
4.none of the above

the answer the trail software suggests is 3
but i dint understand the reason why exception occurs..
please help
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The signature of doIt method of superclass throws exception and in your main method you are calling doIt method using superclass reference,
This will give compile time error as exception has to be handled or the main method should declare throws clause.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public int doIt(String str, Integer... data)throws Exception
{}

whenever you call a method that throws exception..the calling method should handle or declare the exception
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pavithra,

Please QuoteYourSources and UseCodeTags
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mumtaz Khan wrote:
This will give compile time error as exception has to be handled or the main method should declare throws clause.



Mumtaz is correct.

The reference type determines the methods you can call. As the reference sb is of type SuperClass, the compiler assumes you are trying to invoke the SuperClass' method doIt(). As that method throws an Exception, you have to deal with that on the call.

In runtime, it checks the object type and invokes the overriding method, that will not throw any exception.

Is that your doubt pavithra srinivasan?
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per my understanding the compile time error is because

the SuperClass method throws an Exception but the overriden method is not throwing it.

The rule of overriden method says that if a parent method throws an exception then the overriden method can throw same exception or child of the exception or a runtime exception.

So to make the code work you need to add a exception as stated above.
 
pavithra srinivasan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankyou everyone
i understood the point
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshit Rastogi wrote:As per my understanding the compile time error is because

the SuperClass method throws an Exception but the overriden method is not throwing it.

The rule of overriden method says that if a parent method throws an exception then the overriden method can throw same exception or child of the exception or a runtime exception.

So to make the code work you need to add a exception as stated above.



I think you are missing a point here.The overridden method can throw same,narrower(sub-class of the exceptio), runtime exception or no exception at all.

the SuperClass method throws an Exception but the overriden method is not throwing it.

This is not the reason,read the explanations provided, it clearly explains the exception occurs as the super class reference type is used to invoke the method. There is no exception if sub-class reference var type is used to invoke the method as the overridden method in the sub-class does not throw any excetion at all.

Hope this clears the doubt..
 
pavithra srinivasan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes! i tried compiling the method with.



and the program executed without any errors.

you can throw same exceptions or narrower once or runtime exceptions.. or not throw any exceptions at all
but when you don't throw(declare or handle) exception.. then calling method i.e, main method must handle it!

thankyou guys!

 
Guilherme Bazilio
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


pavithra srinivasan wrote:
you can throw same exceptions or narrower once or runtime exceptions.. or not throw any exceptions at all but when you don't throw(declare or handle) exception.. then calling method i.e, main method must handle it!



Even when the overriding method throws a valid exception, you would have to handle that.

;P

 
pavithra srinivasan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ above Giulherme bazilio

what you mean is by doing the following correction the problem will be solved ?




even this compiles and works fine..
 
Guilherme Bazilio
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correction of the compiler error depends on whether you handle the exception or not.

Adding the throws clause to the overriding method will not solve the problem.

When you call the method, the reference used is of type SuperClass and SuperClass' doIt() method throws an Exception that you must handle when calling the method. This is what the compiler checks for. If the compiler sees that you don't handle the Exception thrown, so you'll get an error.

Is that more clearer? Post again anyway.. :P

Thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic