Java - protected members obtained in a derived class using an instance of a base class

I created an instance of a base class in a 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 } } 

Derived class in another 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 can I access a protected member of a base class in a derived class using an instance of the base class when the derived class is also in one package, but not when the derived class is in another package?

If I mark the protected member as β€œstatic”, then I can access the protected member of the base class using an instance of the base class in a derived class that is in another package.

+6
java protected access-control
source share
6 answers

You are right that you cannot do this. The reason you cannot access the field is because you are not in the same package as the class, and you are not getting access to the inherited member of the same class .

The last point is critical - if you wrote

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

then this will work as you change the protected member of your class (which is present in this class through inheritance). However, in your case, you are trying to change the protected member of a class to another in another package. Thus, it is not surprising that you were denied access.

+10
source share

Java tutorial says:

A protected modifier indicates that a member can only be accessed in its own package (for example, in a private package) and, in addition, a subclass of its class in another package.

And in your case, you get access to the variable in another object. Coincidentally, it has a class that is the same as the current one, but visibility checks will not verify this.

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

+3
source share

If a member of the protected class, then there are 2 cases:

  • If the subclass is in the same package
  • If the subclass is in another package

I. The same package:
- Can be accessed through inheritance
- You can access by creating an instance of the parent class
II. Different package:
- Can only access through inheritance

See table below for all use cases:

enter image description here

+3
source share

In short, this is not real. It looks like you should redefine your design.

However, there is work if you are sure you want to do it. You can add a protected method to MyCollection , which takes an instance and sets the intg value 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 keep in mind that this is a very strange way to do this. I would advise once again that your design needs to be reviewed before you go along this route.

+1
source share

According to the Java element accessibility rule, you cannot access a 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 a new instance, try assigning a value. He will work.

0
source share

You cannot access a protected variable in a derived class (which is in another package) when accessed using a new object of the MyCollection class. you can simply write intg = 1; directly without creating (new MyCollection) for example:

 package secondary; import com.core.MyCollection; public class MyCollection2 extends MyCollection{ public void test(){ intg = 1; } } 
0
source share

All Articles