• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected member access

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I got compilation error even for first abc in your code. You please check the code you have given.
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure! then why am I not getting any compilation error at line
System.out.println(abc);
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,
Thanx! for very good explanations...
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome, Soni. Any time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic