Tuesday, January 23, 2007

difference between protected and friendly access specifier

Protected data and methods are accessible to all the classes within the same package and to all the subclasses of any package. Now if this holds true, why the following prog. failed to compile.


// file A.javapackage p1;
public class A{
protected int i = 10;
public int getI() {
return i;
}
}
// file B.java package p2;
import p1.*;
public class B extends p1.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() );
}
}

SOURCE : www.referjava.com

No comments: