This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi, A mock exam question: Which of the classes should have a finalize method? a. A class that creates a socket b. All classes c. Any class that extends a class having a finalize method d. Any class that creates an array of primitive values Ans : a,c IMO, finalize methods are associated with Objects. JVM would call finalize() for each object before being garbage collected. Please correct me if I am wrong. All classes by default will have finalize(), unless explicitly coded, hence I think ans (b) is also correct in addition to a,c. Can anybody clarify this? Thanks Suresh [This message has been edited by Suresh Ray (edited March 21, 2000).]
Nilesh Parikh
Ranch Hand
Joined: Mar 14, 2000
Posts: 52
posted
0
hi Suresh, In my opinion the correct answers are a) and c) and NOT b) As per my knowledge all the object DO NOT HAVE the finalize() method, so the option b) is wrong help me if I am wrong Alkesh
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
All classes do have a finalize() method already, which they inherit from Object. It doesn't do anything though, unless a class overrides the finalize() method to make it do something. So, the question is meaningless unless we interpret it as follows: Which of the classes should override the finalize method? a. A class that creates a socket b. All classes c. Any class that extends a class which overrides finalize() d. Any class that creates an array of primitive values A is still reasonable - the socket should be closed. But why C? If the superclass has a finalize method that actually does something, great - that means that the subclass will inherit the same method, and whatever task needed to be done by the superclass, will still get done by the subclass. Why would we need to override it again? Only if there is some new behavior of the subclass which may require "cleanup" - like the creation of a socket. But that certainly does not apply to every class which extends a class which overrides their finalize() method. So, the answer should be A only, I think. What mock exam is this from, anyway? Post the source (and question number if you have it) and we can put this in the Mock Exam Errata forum. Thanks.
"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
What is exactly meant by "should have" is not very clear to me. I am not sure if anybody has tried to challenge a) and c) along with the rest of the answers. Why should there be a finalize method in a class that creates a socket? What happens if I dont define the finalize() method in the subclass that extends the class that defines the finalize() method. Shouldn't the finalize() method be inherited from the superclass? The finalize() method is defined in the Object class but doesn't perform any action hence only classes that needs to perform any action before garbage collection need define this. Can anyone interprete this question properly?
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Thanks Jim, I realized that you had already posted your reply while I was composing mine. Anyhow we both seem to be bothered by the same questions and the question sure looks a little odd to me.
Suresh Ray
Ranch Hand
Joined: Feb 21, 2000
Posts: 55
posted
0
Thanks. I think I read this question in Exam Cram book. I am not sure. Let me confirm about the source and I will post the info in the Mock Exam Errata forum. Regards Suresh
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Or, if you just add the info to this thread, one of us can move the whole thread to the other forum. (Less typing, right?) And we can change the subject line to make it more easily searchable. Thanks.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.