File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize() method

 
Chun Wang
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.
are all the user-defined class considered to be in the same PACKAGE as the Object class? my guess is yes. because you can access the protected finalize() method via a SUPERclass object reference "super". this following code compiles fine. so that means if you are in the same package as the protected member,you can access it even if it's in a superclass object. only when you are outside of that package can you NOT access the protected member of a superclass object.

please confirm my understanding. thanks for all the help.
chun
------------
protected void finalize() throws Throwable {
System.out.println("okay");
super.finalize();
}
---------------------
 
Velmurugan Periasamy
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, by saying super.finalize(), you are invoking the super class implementation of finalize, but through your own inheritance hierarchy, so there is nothing wrong in it.
All the user-defined classes don't have to be in java.lang where Object class resides.
Remember the accessibility rules for protected - any class in the same package and subclasses outside the package( only via the subclass's inheritance hierarchy)
HTH


------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Chun Wang
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
consider the following code: (this compiles and runs fine)
-------------------------
class Sup {
protected int c =99;
}
class Sub extends Sup {
public static void main(String[] args) {
Sup s = new Sup();
System.out.println("protected c = "+s.c);
}
}
-----------------------
it prints out "protected c = 99".
so this means if you are in the same package, you can even access the protected members of the super class object.
correct me if I am wrong. thanks for your help.
chun
 
Velmurugan Periasamy
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if it is in the same package a subclass (or even any class)can access a protected member.
protected is less restrictive than default package access. All classes in the same package can access the members with default access. protected gives the same freedom, and it goes beyond the package for only the subclasses (and via the hierarchy only)
HTH

------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Cherry Mathew
Ranch Hand
Posts: 159
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chun
I think what u asked is the access of protected members outside the class.
Object and the user defined classes are in seperate packages
try compiling this code and you will get your doubts cleared
public class Test {
public static void main(String args[]){
new Object().finalize(); // will give compiler error
}
}
class Superclass {
protected void meth(){}
}
public class Test extends Superclass{
public static void main(String args[]){
new Superclass().meth(); // wont give compile error because superclass and Test are in the same package.

}
}
Cherry
 
shabbir zakir
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
Proctected members are accessible in the package containing this class and by all subclasses of this class in any package where this class in any package where this class is visible. In other words non -subclasses in other packages cannot access protected members from other packages .It is less restrictive than the default accessibility
 
shabbir zakir
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
Proctected members are accessible in the package containing this class and by all subclasses of this class in any package where this class in any package where this class is visible. In other words non -subclasses in other packages cannot access protected members from other packages .It is less restrictive than the default accessibility.Hope it is clear
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic