java - protected members accessed in derived class

2019-03-13 21:21发布

问题:

I created instance of base class in derived class and tried to access protected members.

I can directly access protected members in a derived class without instantiating the base class.

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
 }
}

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?

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.

回答1:

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.



回答2:

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)



回答3:

If a class member is protected then there are 2 cases:

  1. If subclass is in same package
  2. If subclass is in different package

I. Same package :
- Can access through inheritance
- Can access by creating an instance of parent class
II. Different package :
- Can only access through inheritance

See the table below for all use cases:



回答4:

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.



回答5:

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.



回答6:

You cant access a protected variable in a derived class (which is in different package) if accessed using a new object of class MyCollection. you can just write intg = 1; directly without making ( new MyCollection ) like this:

package secondary;
import com.core.MyCollection;       
public class MyCollection2 extends MyCollection{ 

 public void test(){

     intg = 1;

 }
}