Results 1 to 3 of 3

Thread: protected

  1. #1
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default protected

    "Make the fields or methods protected if they are intended for the extenders of the class but not the users of the class."

    Don't understand who suppose to be "the users of the class" in this context?
    It makes sence to me only if "users" are instances of the class with protected members, inside class from different package.

  2. #2
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default Re: protected

    Basically, as long as you know how to access the child of the parent's class, protected can be abused and let you access that parent's variable directly and even changing it. I did not use protected in my assignments at all because I found this exploit

    For example, Shape is the parent of Triangle and in the Shape class there is a variable called size, I can call this in my main:

    Triangle t1 = new Triangle();
    t1.size = 100;

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: protected

    Quote Originally Posted by diamonddragon View Post
    "Make the fields or methods protected if they are intended for the extenders of the class but not the users of the class."

    Don't understand who suppose to be "the users of the class" in this context?
    It makes sence to me only if "users" are instances of the class with protected members, inside class from different package.
    The "users" are other classes that use objects of this class or its children.

Similar Threads

  1. Why can't I reach the protected member ?
    By fatabass in forum New To Java
    Replies: 11
    Last Post: 01-27-2012, 10:43 PM
  2. protected data member
    By javanoobe in forum New To Java
    Replies: 11
    Last Post: 04-15-2011, 10:09 PM
  3. Top level Protected Classes
    By Jithu in forum New To Java
    Replies: 2
    Last Post: 10-06-2010, 02:28 PM
  4. protected modifier ? Constructors
    By javanew in forum New To Java
    Replies: 6
    Last Post: 04-06-2010, 12: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
  •