Results 1 to 8 of 8
  1. #1
    Swapneel is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default When can we make a method protected?

    protected void finalize() is amethod which is called just before garbage collection is done.
    I want to know why is this method protected?



    regards
    Swapneel

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: When can we make a method protected?

    Protected means - same package or by inheritance, so you would say finalize() is protected if you want to reference it from another Class inside the same package.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default Re: When can we make a method protected?

    The finalize() method is supposed to close/remove resources etc. by/from the object. Suppose it were public or package scope accessible; every moron could call this method with disastrous results. b.t.w. don't forget to call super.finalize() in your finalize() method near the end of its body, so that the object will be 'dismantled' from the outside in.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: When can we make a method protected?

    Quote Originally Posted by JosAH View Post
    Suppose it were public or package scope accessible;
    kind regards,

    Jos
    But 'protected' is package scope accessible.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: When can we make a method protected?

    It's the best they could achieve.
    It's the only one that will allow any subclasss to override and access the parent method, as well as restricting it so not every other class could.

    Besides, you pretty much never have to write one of these things.
    I've never written one in 15 years.
    Please do not ask for code as refusal often offends.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default Re: When can we make a method protected?

    Quote Originally Posted by jim829 View Post
    But 'protected' is package scope accessible.

    Regards,
    Jim
    Yep, you're right (I forgot about that); that is only because they had to ruin protected access rights compared to packaege access rights, i.e. you can subclass a class in another package; but the finalize() method still isn't publically accessible (and it shouldn't be ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: When can we make a method protected?

    Also, something Toll's said earlier. I can't remember (if ever) when I have used protected as an access modifier. I pretty much use only public and private. Perhaps one day I will find the need.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default Re: When can we make a method protected?

    I use it on a regular basis but I always forget that 'protected' is 'wider' than 'package' access. I bluntly use the protected methods in subclasses only (I've always been an obient little puppet ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 03-25-2012, 09:24 AM
  2. How to make a morph Method
    By AnthonyTTaylor in forum Java 2D
    Replies: 6
    Last Post: 11-15-2011, 11:41 PM
  3. I want to make an array in a method...
    By nhmllr in forum New To Java
    Replies: 7
    Last Post: 10-02-2011, 08:18 AM
  4. help with protected method in vector class
    By katie in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 10:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •