I don't understand this question.
Why cannot we access the instance variable when they are in different packages. While we can access the same variable when they are in the same package.
If B is not involved in the implementation of A, the behavior should be the same whether or not the classes are in the same package.
Code: Select all
package be.p2;
public class B extends A {
public void process(A a) {
a.i = a.i * 2;
}
public static void main(String[] args) {
A a = new B();
B b = new B();
b.process(a);
System.out.println(a.getI());
}
}
package be.p2;
public class A {
protected int i = 10;
public int getI() {
return i;
}
}
Thanks a lot for you help.