java - protected members accessed in derived class using base class instance

十年热恋 提交于 2019-12-03 00:36:55

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.

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)

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:

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.

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.

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;

 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!