• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

finalize() method

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
---------------------
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'm full of tinier men! And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic