aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading and overriding the finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading and overriding the finalize method" Watch "Overloading and overriding the finalize method" New topic
Author

Overloading and overriding the finalize method

Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Hi all,
I read in one of the books that, it is not possible to overload the finalize() method. Also that, in the case of overriding the finalize() method, the access modifier of the overriding method should not be more restricted than the 'protected' access modifier.
In the following example, I tried overloading the finalize() method. Compiler does not complain
I also tried to override the finalize() method, the overriding method having the default(friendly) access modifier, which is not less restricted than the protected modifier. In this case the compiler flags an error.

Can anybody please explain as to why the compiler 1. is allowing overloading of finalize() method?
2. flags an error if the access modifier is less restrictive than the protected access modifier?
Thanks a lot.
Kezia.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
1. overloading is allowed for any method. finalize is just a method after all.
2. default (friendly) is more restrictive than protected and that's why the compiler complains.
HIH
[ January 19, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
I don't agree with you

default (friendly) is more restrictive than protected and that's why the compiler complains

I can prove my point.
If in the same package, I have 2 separate (independent) classes (say class A and class B), I can access the protected member of one class (say 'A') from another ('B'), even though, I do not extend from A in B.
If I have 2 packages and have class A in one and class B in the other, I would not be able to access the protected member of A from B, if I do not extend A in B.
This shows that default is less restricted than protected. Feel free to correct me, if I am wrong.
If this is true, then why does the compiler flag an error when I tried to override the method finalize() with the default access modifier?
Thanks.
Kezia.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Kezia Matthews:
If this is true, then why does the compiler flag an error when I tried to override the method finalize() with the default access modifier?

Because it's not true.
in order of most accessible to most restrictive, the access modifers are ranked:
1. public - any class in any package can access the member
2. protected - any class in the same package can access the member, as well as any subclasses of the member's class, in any package.
3. (default - no explicit modifier keyword) - I like to think of this as "package." If you do the same, you'll never be confused again. The "package" access modifier (default) only allows classes in the *same* package to access the member. Subclasses that are in a different package cannot access the member!
4. private - only the class itself can access the member.

Now, that being said, I hope you can see why the compiler is complaining.
As for Overloading, you may overload the finalize() method to your heart's content
String finalize(SuperNutrinoParticleObject s){}
int finalize(int i);
float[] finalize(String a, String b, Thread t);
etc.
The garbage collector only calls the finalize() method with the signature:

protected void finalize()
throws Throwable
and no other. Your code is free to call these overloaded versions as you see fit.
Rob
[ January 19, 2002: Message edited by: Rob Ross ]
[ January 19, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
No I don't agree with you.
default members can only be accessed throughout a package. Protected members can be access throughout a package and outside a package if the class extends the class containing the protected member. That is you can access default members only in a package while you can access protected members inside a package AND outside if you are subclassing the class declaring the protected member.
The most restrictive access modifier is private, then comes default, then protected and finally public.
To back up my thought, have a look at JLS 6.6 Access Control
HIH
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Thanks for correcting me.
In the code above which I posted, both classes 'A' and 'B' belong to the same package.
I still don't understand as to why the compiler gives an error when I try to compile the finalize() method with the default (package) modifier. If the finalize() method is made public, the compiler does not flag the error.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Because you cannot override a method to be more private !!! Default ("package") access is more private than protected and that's why the compiler complains ...
HIH
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107


Thanks anyway.
Kezia
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Did you get it or do you need some more explanations? (asking that because of the "Thanks anyway")
[ January 20, 2002: Message edited by: Valentin Crettaz ]
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
No more explanations required. I understood it. Thanks a lot.
Kezia.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading and overriding the finalize method