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;
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;
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.
java protected access-control
Tarun
source share