aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected member access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected member access" Watch "protected member access" New topic
Author

protected member access

Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
I read that a protected member of parent class works as a private inharited member of child class but when I am trying like this:

package Parent;
public class Parent{
protected String abc;
}
----------------
package Child;

import Parent.Parent;

public class Child extends Parent{
private String xyz;
}
------------------
1. package Child;

2. public class Neighbour1 extends Child{
3. public void test(){
4. System.out.println(abc); //No problem on compilation
5. System.out.println(xyz); //ERROR: xyz has private access in Child.Child
6. Child c = new Child();
7. System.out.println(c.abc);//ERROR: abc has protected access in Parent.Parent
8. }
9. }

Why is there no problem when I am accessing abc directly in Neighbour1 class at line no. 4 ? If abc is private member of Child class then it should have create compilation error.
[ April 12, 2005: Message edited by: Soni Prasad ]

SCJP 1.4, SCBCD 1.3
challa
Greenhorn

Joined: Mar 28, 2005
Posts: 23
Hi
I got compilation error even for first abc in your code. You please check the code you have given.
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
Are you sure! then why am I not getting any compilation error at line
System.out.println(abc);
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,
I think sai do some other mistakes. It also compile fine to me.
In subclass from other packages we can access the protected members
through inheritance only. We can't access throgh object instance.
So Line no 7 shows an error message.


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
The problem is that a child can only access a parent's protected member THROUGH INHERITANCE. A child cannot access a parent's protected member by using an instance of the parent.

Think of it this way:

A child object outside the parent's package cannot access the protected member of an instance of the parent class.

However, the child does INHERIT the protected member as a part of the child. The child can access the member that has been inherited and is a part of the child object.

Since Neighbour1 is not in the Parent package, the instance of Neighbour1 that is calling test() cannot access the protected member of Parent.Parent by using an instance of Child. The only abc instance variable that the Neighbour1 object can access is the one that is an instance variable of Neighbour1, which was passed down through inheritance.

I assume your example comes from Kathy Sierra & Bert Bates' book, pages 79-81. They do say there that the protected member becomes private, and later they call it "essentially private", although that is not strictly true because of one case that they do not discuss, which I'll cover here:

Interestingly, if you move Neighbour1 to the Parent package and you import Child.Child in Neighbour1.java, then the error on line 7 goes away. This is because classes in the same package can freely access each other's protected members. So why can Neighbour1 access this "essentially private" member of Child, if Child is in a different package from Neighbour1? Because the package where abc was declared is the Parent package. Any class within that package where abc was declared can access abc, no matter what class abc actually belongs to (in this case, Child.Child).

This level of complexity may not be necessary to know, but if you can grasp this situation, and the one you first posted, then you've got a good understanding of what protected really means.



I hope my long-winded explanation helps steer you toward an understanding of the issues involved.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi joe
i just concluded what u have said.
hav a look at it.

The protected member(P.M) is accessed through the inheritance only.

for a class to access the protected member in other way should be a class in a package where the P.M is declared.

The P.M is like a private var in a class in which it is extended but still it can be extend to other sub-class too.

if a sub-class has a member variable in the same name as one in super class then the shadowing will take place.local var will precede over the inherited variable.

i had one doubt here....

what about the protected method is it overridden or overloaded by the sub-class if one declared in the sub-class, or it will throw error.
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
Hi Joe,
Thanx for a very good explanation and clearing the issues but till now I am unable to understand that why there in no compilation error on line

System.out.println(abc);

in Neighbour1.class even if I put it in different package then the Child class?

---------------------
package Neighbour;
import Child.Child;

public class Neighbour1 extends Child{
public void test(){
System.out.println(abc); //No problem on compilation
System.out.println(xyz); //ERROR: xyz has private access in Child.Child
Child c = new Child();
System.out.println(c.abc);//ERROR: abc has protected access in Parent.Parent
}
}
---------------------

If abc is a private member of Child class then it should have given compilation error.
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
Here is some explanation:

there are 4 accesibility options for members:

- default : no accessibility modifier : (ie: String abc member is accessible from classes within the package only. Now, the classes can be inheriting the containing class or just instantiating the containing class but should be a part of the same package.

- private: private means private!!! this is a conceptual problem i see in the question asked above. A private member can ONLY be accessed from within the same class and no where else!!!

- protected: member can be accessed from the sub classes of containing class, and classes within the same package.

- public: member can be accessed from any class where the containing class is accessible.

Hope this helps.

Saurabh
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
I think you've grasped what I was saying. I don't really agree that the protected variable has much in common with a private variable, since private variables can't be accessed by anything at all outside the class where they're declared. Protected access is closer to default access (a.k.a. package-level access) except that protected allows subclasses to inherit and use the variable.



[ April 12, 2005: Message edited by: Joe Sanowitz ]
[ April 12, 2005: Message edited by: Joe Sanowitz ]
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by Soni Prasad:
...till now I am unable to understand that why there in no compilation error on line

System.out.println(abc);

in Neighbour1.class even if I put it in different package then the Child class?


You're welcome, Soni. I'm glad I could help clear up some of this stuff. The reason there is no compilation error on that line is that the code is accessing abc through inheritance, which works because abc is a protected variable.

The Parent class declares abc as a protected instance variable. Since Parent has abc, so do the descendants of Parent.

The Child class inherits abc from Parent, so abc is an instance variable of Child. Since abc is inherited and it's protected, Child has access to its own abc instance variable. Since Child has abc, so do the descendants of Child.

Neighbour1 inherits abc from Child, so abc is an instance variable of Neighbour1. Since abc is inherited and it's protected, Neighbour1 has access to its own abc instance variable. That's the variable being accessed in that line of code you mentioned, so there's no problem with that line.

Does that make sense?
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
Hi Joe,
Thanx! for very good explanations...
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
You're welcome, Soni. Any time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected member access
 
Similar Threads
Confused on protected modifier
Doubt in protected modifier
problem on extending a class declared on the same package!!
protected acces modifier
default vs protected access