• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize access specifiers public/protected/private

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,
Refer the example #1 "Finalize.java" Change the finalize method with following piece of code
ie.
void finalize(){
System.out.println("finalize-TestA");
}
OR
private void finalize(){
System.out.println("finalize-TestA");
}

The question is, If you do change, you will get compilation error.
Please help me to understand, why do we get compilation error, if we use access specified other than public or protected.
Hope my posting make sense.
Thanks in Advance
Siva

*********** Example #1 **************************
// Finalize.java

class TestA {
TestA(){
System.out.println("TestA");
}
void Hello(){
System.out.println("Hello-TestA");
}

protected void finalize(){
System.out.println("finalize-TestA");
}
}
class Finalize {
Finalize(){
System.out.println("Finalize");
}

public static void main(){
System.out.println("finalize-TestA");
}
}
********************************************
 
Deep Chand
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sivanantham ,
"protected void finalize()" is a method in Object class, which is a superclass of every class.
The access specifier in a method that overrides a method in the super class should be same or more than than access in the method in the super class. So the finalize() method in your class can be protected or public. It can't be package or private.
cheers,
deep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic