Access level modifiers question

I am having trouble learning about different Access level modifiers i have read this page http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html and thought i would try some sample questions online but am having a hard time getting them right could somebody please help me with this?

package Family;

publicclass Parent{

protected String protectedVar ="Protected";

String packageVar ="Package";

}

publicclass Uncle{

publicvoid SampleAccess( Parent parameter ){

parameter.protectedVar ="Line 1";

parameter.packageVar ="Line 2";

}

}

package Quiz;

publicclass Aunt{

publicvoid SampleAccess( Family.Parent parameter){

parameter.protectedVar ="Line 3";

parameter.packageVar ="Line 4";

}

}

publicclass Childextends Family.Parent{

publicvoid SampleAccess( Family.Parent parentType,

Child childType){

parentType.protectedVar ="Line 5";

parentType.packageVar ="Line 6";

protectedVar ="Line 7";

packageVar ="Line 8";

childType.protectedVar ="Line 9";

childType.packageVar ="Line 10";

}

}

The question says list the illegal assignments, with a description of why each one is illegal. Can somebody please tell me which lines are illegal? I would prefer if you didn't tell me why so i can figure that out for myself.

[2691 byte] By [ajrobsona] at [2007-11-27 4:20:19]
# 1
> Can> somebody please tell me which lines are illegal? The compiler can. :-)After you try it, if you don't understand the compiler's responses, post your specific questions and why you disagree or don't understand.
jverda at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 2

> > Can

> > somebody please tell me which lines are illegal?

>

>

> The compiler can. :-)

A salient point. Not just in this case, but in general. A good amount of the woes lamented on these boards could be avoided if people learnt to understand compiler output.

georgemca at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 3
Yeah i was gunner do that but got lazy lol its too sunny! a quick question though if i compile these i'll need to make a class with a main right? but in what package would that go?
ajrobsona at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 4
> Yeah i was gunner do that but got lazy lol its too> sunny! a quick question though if i compile these> i'll need to make a class with a main right? but in> what package would that go?Whichever package you choose
georgemca at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks i'll do it now.
ajrobsona at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 6

> Yeah i was gunner do that but got lazy lol its too

> sunny! a quick question though if i compile these

> i'll need to make a class with a main right? but in

> what package would that go?

It only has to have a main if you try to run it. Just to test the legality of the syntax, you can put them in a class with or without a main.

jverda at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 7

Ok i ran them and got them all right apart from one can somebody explain it to me please:

Quiz/Child.java:5: protectedVar has protected access in Family.Parent

parentType.protectedVar = "Line 5";

Why does that get an error it is protected in Parent, this line comes from Child class which extends parent making child a subclass right? but according to the page i went to above a protected thing can be accssed in the same class,package and subclass so why does it list that error?

ajrobsona at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 8

Protected access means that a child that is in a different package has access to the field that it inherits from the parent. Otherwise the access is restricted to the package: no class from a different package can access parentType.protectedVar.

There used to be a decent explanation of this in the tutorial but in accordance to this website's policy of making the service gradually worse it has been removed at some point.

jsalonena at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 9
Thanks I am still a bit iffy on this though, the child class extends parent so why does it not inherit protectedVar string?
ajrobsona at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 10
The child does inherit the protected variable. It has a copy of its own and you can access it just fine as you can see on the lineprotectedVar = "Line 7";This is the only kind of access across packages that "protected" allows.
jsalonena at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 11
ok then whats the difference between protectedVar and parent.protectedVar? sorry for asking but I am having trouble understanding this
ajrobsona at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 12

> The child does inherit the protected variable.

I believe it does. Without reading the question or the JLS in detail, I believe it comes down to this:

package p;

class Parent {

protected String foo;

}

package c;

import p.*;

class Child extends Parent {

Parent p;

void bar() {

String s1 = p.foo; // NO

String s2 = foo; // OK

String s3 = super.foo; // OK (I think)

}

}

[url http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2]JLS 6.6.2 Details on Protected Access[/url]

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

6.6.2.1 Access to a protected Member

Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:

If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.

If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

6.6.2.2 Qualified Access to a protected Constructor

Let C be the class in which a protected constructor is declared and let S be the innermost class in whose declaration the use of the protected constructor occurs. Then:

If the access is by a superclass constructor invocation super(. . .) or by a qualified superclass constructor invocation of the form E.super(. . .), where E is a Primary expression, then the access is permitted.

If the access is by an anonymous class instance creation expression of the form new C(. . .){...} or by a qualified class instance creation expression of the form E.new C(. . .){...}, where E is a Primary expression, then the access is permitted.

Otherwise, if the access is by a simple class instance creation expression of the form new C(. . .) or by a qualified class instance creation expression of the form E.new C(. . .), where E is a Primary expression, then the access is not permitted. A protected constructor can be accessed by a class instance creation expression (that does not declare an anonymous class) only from within the package in which it is defined.

Like I said, I'm just guessing, but this kind of question comes up a lot. Protected is slighly non-intuitive in this way.

jverda at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...
# 13

> ok then whats the difference between protectedVar and

> parent.protectedVar? sorry for asking but I am having

> trouble understanding this

"protectedVar" is a variable of the Child class, inherited from the the Parent class. You can access it through the names protectedVar, this.protectedVar, and super.protectedVar.

"parent.protectedVar" is a variable of an object of the Parent class. Access to it is limited to the package of the Parent class.

Maybe the tutorial is a little misleading when it says

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

A subclass in another package does not gain access to the protected variables of objects of its parent class (nor sibling classes); it gains access only to the variable it inherits. The variable "parent.protectedVar" is an example of the former, and "this.protectedVar" of the latter.

jsalonena at 2007-7-12 9:27:19 > top of Java-index,Java Essentials,New To Java...