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.
 
     
     
    
 
     
     
     
    