public class MyTest {
public static void main(String str[]){
Info i=new Info();
i.value=20;
System.out.println("Integer value is :"+i.value);
}
}
class Info {
int value;
protected Info(){
System.out.println("Class with protected constructor");
}
}
-->I am missing something about protected modifier but could not figure it out. -->Till now i have read that a class with protected constructor can only be instantiated by its Sub-Classes and that too in a same package or in a different package. Then how come the above is giving me this output : Class with protected constructor Integer value is :20
Protected
members are accessible from within Class
, Same package
and in Subclass
. It doesn't have access to the World
(other than mentioned above)
See the below table
The following table shows the access to members permitted by each modifier.
Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N