//in file A.java
package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}
//in file B.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2; //line 1
}
public static void main(
String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println( a.getI() );
}
}
Result: Compilation error, i has protected access in p1.A
//line 1 is giving error
/* A protected member is accessible in all classes in the package containing its class, and by all subclasses of its class in any package where this class is visible. */
here, B is the subclass of A. Then why this programe is giving compilation error? can any one of you please explain me.
Thanx in advance
Naresh