Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Why can't I reach the protected member ?

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why can't I reach the protected member ?

    Java Code:
    package myPackage1;
    
    public class Human 
    {
    	int defaultaccess;	
    	public int publicaccess;
    	protected int protectedaccess;
    	private int privateaccess;
    }
    Java Code:
    package myPackage2;
    
    import myPackage1.Human;
    
    public class testc extends Human
    {
    	public static void main(String[] args) 
    	{
    		Human Norm = new Human();
    		Norm.protectedaccess = 4;
    	}
    }
    Eclipse is telling me to change visibility of protectedaccess to protected. But it is protected already. What am I doing wrong ?

    When I force Eclipse to compile and run:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The field Human.protectedaccess is not visible

    at myPackage2.testc.main(testc.java:10)

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can't I reach the protected member ?

    When I do this:

    Java Code:
    	public static void main(String[] args) 
    	{
    		testc Norm = new testc();
    		Norm.protectedaccess = 4;
    	}
    It seems to work fine. But what am I doing wrong when I go with Human norm = new Human() ?

    Why can't I reach the protected member by that way ?

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Why can't I reach the protected member ?


  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can't I reach the protected member ?

    How does that answer my question ?

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

    Default Re: Why can't I reach the protected member ?

    Quote Originally Posted by fatabass View Post
    How does that answer my question ?
    Are you serious? It provides the direct answer to your question. Please review the link provided by doWhile.
    doWhile likes this.

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can't I reach the protected member ?

    My testcc class extends the Human class. ( Which is in a different package. )

    It has a private int privateaccess.

    When I create a Human object in testcc class, I cant access this private int privateaccess.

    When I create a tetscc object, then I can access private int privateaccess.

    Why ?

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Why can't I reach the protected member ?

    Quote Originally Posted by fatabass View Post
    How does that answer my question ?
    Seriously? You've given a variable protected access...meaning its visibility is limited. Think about it.

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can't I reach the protected member ?

    My question is:

    Why can't I reach it, when I create a Human object ( of type Human ), in a class which extends the Human class ?
    Last edited by fatabass; 01-27-2012 at 09:21 PM. Reason: not can, can't

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,385
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why can't I reach the protected member ?

    Private is private on a class level; if class D extends a class B, no object of class D can access a private member of any object from class B; this, of course hasn't much to do with proteced access which is clearly descrbed in the link supplied to you (see above).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Why can't I reach the protected member ?

    Quote Originally Posted by fatabass View Post
    My question is:

    Why can I reach it, when I create a Human object ( of type Human ), in a class which extends the Human class ?
    With the speed in which you are replying, I am under the impression you did not read the link I gave you above. You cannot access a protected variable outside of the package or a subclass.

  11. #11
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why can't I reach the protected member ?

    Quote Originally Posted by doWhile View Post
    With the speed in which you are replying, I am under the impression you did not read the link I gave you above. You cannot access a protected variable outside of the package or a subclass.
    This I understand clearly.

    Ok, I am outside the package, but my question is: I am in a Subclass ?

    my testcc class extends the Human class.
    and in this sub - class, I have ( Human Norm = new Human();)

    Why cant I say Norm.protectedaccess ?

    but why am I allowed to say:

    testcc Norm = new testcc();
    Norm.protectedaccess ?

    What is the difference ?

  12. #12
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Why can't I reach the protected member ?

    See the following section of the Java spec, which addresses this point:
    http://java.sun.com/docs/books/jls/t...mes.html#6.6.7

Similar Threads

  1. How do I reach my goal?
    By DanishKid in forum New To Java
    Replies: 1
    Last Post: 07-29-2011, 12:36 AM
  2. protected data member
    By javanoobe in forum New To Java
    Replies: 11
    Last Post: 04-15-2011, 09:09 PM
  3. Reach a variable outside block.
    By ocean in forum New To Java
    Replies: 8
    Last Post: 10-30-2009, 01:08 PM
  4. Java failed to reach to a PC in local LAN
    By JavaEmpires in forum Networking
    Replies: 1
    Last Post: 01-24-2008, 06:30 PM
  5. Replies: 1
    Last Post: 08-07-2007, 07:53 AM

Posting Permissions

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