Why can't I reach the protected member ?
Code:
package myPackage1;
public class Human
{
int defaultaccess;
public int publicaccess;
protected int protectedaccess;
private int privateaccess;
}
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)
Re: Why can't I reach the protected member ?
When I do this:
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 ?
Re: Why can't I reach the protected member ?
Re: Why can't I reach the protected member ?
How does that answer my question ?
Re: Why can't I reach the protected member ?
Quote:
Originally Posted by
fatabass
How does that answer my question ?
Are you serious? It provides the direct answer to your question. Please review the link provided by doWhile.
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 ?
Re: Why can't I reach the protected member ?
Quote:
Originally Posted by
fatabass
How does that answer my question ?
Seriously? You've given a variable protected access...meaning its visibility is limited. Think about it.
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 ?
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
Re: Why can't I reach the protected member ?
Quote:
Originally Posted by
fatabass
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.
Re: Why can't I reach the protected member ?
Quote:
Originally Posted by
doWhile
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 ?
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