• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Finalize ()

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all...
Finalize()
This is from Khalid Mughal book:- page 256 q 8.4
True or False:-
"An Overriding Finalize method in any classs can always throw Checked Exceptions"
The Answer Given was False* Reason:- page 844.
"The finalize() in Object can throw any throwable object Overridding method can limit the range of throwable to
unchecked Exceptions. Further Overridden definitions of this method in subclass will not be able to
throw checked Exception."
But i tried Overriding finalize() with a Checked Exception:-
public void finalize() throws ClassNotFoundException
{
System.out.println("Finalize Called ");
}
The above code works properly
So what am i missing. ?
Thanks
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you override a method that throws a checked exception, your overriding method can not declare a more general exception.
The extension of this is that when you override a method that does not throw any checked exception, the overriding method can't throw a checked exception.
I bet your test case did not exactly represent the class hierarchy that KM was describing.
Bill

------------------
author of:
 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dear Sir...
The Signature of finalize() as given in object class is :-
protected void finalize() throws Throwable
Now from above if i override the method with a Checked
Exception it works ...
But acc to KM it was given false.
Correct me if i m wrong
Thanks
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the situation referred to can be represented as:
Object
+-- firstDerived
+--- secondDerived
Now - if the finalize method in firstDerived does NOT declare any thrown checked exception, then the finalize in secondDerived may not declare any checked exception.
What you appear to be thinking about is:
Object
+---- firstDerived
But that is not the situation in the original question - (my addition):
"The finalize() in Object can throw any throwable object Overridding method (firstDerived) can limit the range of throwable to unchecked Exceptions. Further Overridden (secondDerived) definitions of this method in subclass will not be able to throw checked Exception."
Bill

------------------
author of:
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
arg - I forgot this damn system removes leading spaces.
Object
...+--- firstDerived
.........+---- secondDerived
is what I wanted to show.
 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the Reply...
 
Tell me how it all turns out. Here is a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic