wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize() method" Watch "finalize() method" New topic
Author

finalize() method

Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
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

Joined: Nov 09, 2000
Posts: 95
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/


------------------------------------------------------------------------------------<BR>Velmurugan Periasamy<BR>Sun Certified Java Programmer for Java 2 Platform<BR>Sun Certified Web Component Developer for J2EE platform<BR>Sybase Certified EAServer Developer (similar to jCert level 3)<BR>------------------------------------------------------------------------------------<BR>Study notes for Sun Java Certification<BR><A HREF="http://www.geocities.com/velmurugan_p/" TARGET=_blank rel="nofollow">http://www.geocities.com/velmurugan_p/</A><BR>------------------------------------------------------------------------------------
Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
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

Joined: Nov 09, 2000
Posts: 95
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

Joined: Dec 26, 2000
Posts: 159
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

Joined: Nov 12, 2000
Posts: 183
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

Joined: Nov 12, 2000
Posts: 183
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
 
subject: finalize() method