It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access Modifier Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access Modifier Confusion" Watch "Access Modifier Confusion" New topic
Author

Access Modifier Confusion

George Coolidge
Greenhorn

Joined: Apr 08, 2009
Posts: 11
I am utterly confused by a question in SCJP preparation book:
There are two classes in different packages, say Foo and Foot as below:

package pkgA;

public class Foo{
int a = 5;
protected int b = 6;
}

package pkgB;

import pkgA.Foo;

public class Foot extends Foo{
Foo f = new Foo(); //line 1
System.out.println(f.a); //line 2
System.out.println(f.b); //line 3
System.out.println(new Foot().a); //line 4
System.out.println(new Foot().b); //line 5
}

In the book and also in Ecplise 3.3.0, line 2, 3, 4 will cause compiler errors. In my opinion, line 2 is illegitimate since Foo is in different package of Foot and variable a is package private, so it is not visible in line 2.

However, in line 3, since Foot extends Foo, f.b should be visible to Foot and also new Foot().a should be visible to Foot class since it is referring to a variable of itself. I looked in the Java online documents (http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html), it states that protected members can be accessed by subclasses or classes in the same directory. Could somebody explains to me why line 2, 3 and 4 don't pass the compiler checking? Thank you and I really appreciate it.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Protected member can be accessed in All the classes present in the same package and only by sub classes from different package but with one condition.

Subclass in different package can access the protected member from super class only through inheritance so you should not use Super class reference variable to access the protected member Insted just use the variable name or use sub class reference variable.

This condition holds true only if the sub class is in different package.

Hope you are clear now.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
George Coolidge
Greenhorn

Joined: Apr 08, 2009
Posts: 11
OK, I can understand why line 2 and 3 are illegitimate with your explanation, but why line 3 is still illegitimate? Should class Foot be able to access its own variable provided that it is within the same package? Thank you for your help!

Siva Masilamani wrote:Protected member can be accessed in All the classes present in the same package and only by sub classes from different package but with one condition.

Subclass in different package can access the protected member from super class only through inheritance so you should not use Super class reference variable to access the protected member Insted just use the variable name or use sub class reference variable.

This condition holds true only if the sub class is in different package.

Hope you are clear now.
Anton Ganeshalingam
Greenhorn

Joined: Jun 26, 2007
Posts: 3
Remove the following lines and put them inside a method:

System.out.println(f.a); //line 2
System.out.println(f.b); //line 3
System.out.println(new Foot().a); //line 4
System.out.println(new Foot().b); //line 5
George Coolidge
Greenhorn

Joined: Apr 08, 2009
Posts: 11
I am not asking how to fix the problems, but why these lines don't pass the compiler's checking, thanks.

Anton Ganeshalingam wrote:Remove the following lines and put them inside a method:

System.out.println(f.a); //line 2
System.out.println(f.b); //line 3
System.out.println(new Foot().a); //line 4
System.out.println(new Foot().b); //line 5
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
These are passing compiler checks.

You cannot access

***default and protected are almost identical except one difference ie. a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package but a default cannot.
Anton Ganeshalingam
Greenhorn

Joined: Jun 26, 2007
Posts: 3
George Coolidge wrote:
However, in line 3, since Foot extends Foo, f.b should be visible to Foot and also new Foot().a should be visible to Foot class since it is referring to a variable of itself. I looked in the Java online documents (http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html), it states that protected members can be accessed by subclasses or classes in the same directory. Could somebody explains to me why line 2, 3 and 4 don't pass the compiler checking? Thank you and I really appreciate it.


line 3 //
Just because you extend Foot to Foo doesn't mean variable f is a subclass of type Foo. Variable f is contained ( has-a) in Foot class. You need to understand "is-a" / "has-a" relationships. In the context of Foot class, variable f is pointing to a object of type Foo which is defined in pkgA. This is why line 3 not compiling. When it comes to protected access member, it is visible to all classes that has"is-a" and "has-a" relationships in same package. But outside package a class can access to a protected variable only through inheritance (is-a).

Hope this helps.
George Coolidge
Greenhorn

Joined: Apr 08, 2009
Posts: 11
Thank you for the explanation on the difference of is-a and has-a relationship, I now understand that Foot class cannot even access variable a through inheritance even though Foot extends Foo, and it can access Foot class's variable b since it now has the ownership of variable b now.

Since now Foot class has the variable
Anton Ganeshalingam wrote:
George Coolidge wrote:
However, in line 3, since Foot extends Foo, f.b should be visible to Foot and also new Foot().a should be visible to Foot class since it is referring to a variable of itself. I looked in the Java online documents (http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html), it states that protected members can be accessed by subclasses or classes in the same directory. Could somebody explains to me why line 2, 3 and 4 don't pass the compiler checking? Thank you and I really appreciate it.


line 3 //
Just because you extend Foot to Foo doesn't mean variable f is a subclass of type Foo. Variable f is contained ( has-a) in Foot class. You need to understand "is-a" / "has-a" relationships. In the context of Foot class, variable f is pointing to a object of type Foo which is defined in pkgA. This is why line 3 not compiling. When it comes to protected access member, it is visible to all classes that has"is-a" and "has-a" relationships in same package. But outside package a class can access to a protected variable only through inheritance (is-a).

Hope this helps.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
George Coolidge
Greenhorn

Joined: Apr 08, 2009
Posts: 11
I couldn't find the edit button for my first post, thanks for the reminder.

Ankit Garg wrote:Please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

George Coolidge wrote:I couldn't find the edit button for my first post, thanks for the reminder.


i think[not sure] your post count to be 36[ranch hand] to get edit button[sorry , if i am wrong]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifier Confusion
 
Similar Threads
clarification - protected access rules
Protected and Default members, different packages...Please explain...
confusion of protected
Error on K&B exercise in Chap 10...
accessing a protected access instance variable defined at a superclass