• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access Modifier Confusion

 
George Coolidge
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
George Coolidge
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...
 
George Coolidge
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic