views:

74

answers:

4

Hello All,

I must admit that I have been a manual tester and have just begun swimming through java (for selenium tool) I got to know that protected members of a class would be accessible in derived class. Despite this I created instance of base class in derived class and tried to access protected members (I agree that it sounds foolish as I can directly access protected members in derived class with out instantiating base class but still....). So whole scenario is -

Base class -

package com.core;

public class MyCollection {

      protected Integer intg;
}

A derived class in the same package -

package com.core;

public class MyCollection3 extends MyCollection { 

 public void test(){

  MyCollection mc = new MyCollection();
  mc.intg=1; // Works
 }
}

A derived class in a different package -

package secondary;

import com.core.MyCollection;

public class MyCollection2 extends MyCollection{ 

 public void test(){
  MyCollection mc = new MyCollection();
  mc.intg = 1; //!!! compile time error - change visibility of "intg" to protected
 }
}

I am wondering how it is possible to access a protected member of a base class in a derived class using instance of base class when derived class is also in same package, but not when derived class is in different package.

Herein if I mark protected member as "static" then I am able to access protected member of base class using instance of base class in a derived class which resides in a different package (Though at this point I could have access it using class name itself instead of creating instance of base class)

+3  A: 

You're right that you can't do this. The reason why you can't access the field, is that you're not in the same package as the class, nor are you accessing an inherited member of the same class.

The last point is the critical one - if you'd written

MyCollection2 mc = new MyCollection2();
mc.intg = 1;

then this would work, as you're changing a protected member of your own class (which is present in that class through inheritance). However, in your case you're trying to change a protected member of a different class in a different package. Thus it should come as no surprise that you're denied access.

Andrzej Doyle
This explanation is really easy to understand.I had a look at java language specification and their elaboration went over my head!!!
Tarun
@Tarun - if an answer has worked for you, mark it as accepted (tick below the vote counter)
Bozho
I did not know this feature. I would mark it as answered now.
Tarun
A: 

According to the member accessibility rule of Java you cannot access protected member of a class without extending it.

You can try the following.

package secondary;

import com.core.MyCollection;

public class MyCollection2 extends MyCollection{ 

 public void test(){
  intg = 1; 
 }
}

Instead of creating the new instance try to assign the value. It will work.

Multiplexer
Agree with your reasoning
Tarun
+1  A: 

The Java tutorial 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.

And in your case, you are accessing the variable in another object. By coincidence it has a class that's the same as the current one, but the visibility checks wouldn't check that.

So, the second time you are denied access, because you are in a different package, and the first time you are given access because you are in the same package (and not because it's a subclass)

Bozho
"first time you are given access because you are in the same package (and not because it's a subclass)"This makes it clear for me. Thanks.
Tarun
A: 

In short, it's not really possible. It seems like you should reconsider your design.

However, there's a work around, if you're sure that's what you want to do. You can add a protected method to MyCollection which takes an instance and sets the value of intg on your behalf:

package com.core;

public class MyCollection {

    protected Integer intg;

    protected void setIntg(MyCollection collection, Integer newIntg) {
        collection.intg = newIntg;
    }
}

Now your subclasses can access this method:

package secondary;

import com.core.MyCollection;

public class MyCollection2 extends MyCollection{ 

    public void test(){
        MyCollection mc = new MyCollection();
        setIntg(mc, 1);
    }
}

But please note that this is a very strange way of doing it. I'd suggest again that your design needs to be rethought before you go down this route.

Chris Smith
I get your point.Most easy way for me would have been to directly access the protected member than instantiating base class and accessing it in a different package.I did it "mistakenly" while playing with java (did not I say I have been a manaul tester :-)) and then none of my dev member was able to provide me conceivable answer. So I posted it here.
Tarun