For the above hierarchy, Below is the code from pkg1
,
package ProtectedAccess.pkg1;
public class T {
protected int f;
protected void m(){
}
}
Below is the code from pkg2
, where test2
is tested by replacing X
with class T
or A
or B
or C
or S
or D
or E
or F
or G
or H
.
package ProtectedAccess.pkg2;
import ProtectedAccess.pkg1.*;
class A extends T {}
class B extends T{}
class C extends B{}
public class S extends B{
void test1(){
f = 42;
m();
}
void test2(X t){
t.f = 42;
t.m();
}
}
class D extends S{}
class G extends D{}
class E extends S{}
class H extends E{}
class F extends S{}
--
1)
If X
is replaced with T
or A
or B
or C
, then compiler error.
2)
If X
is replaced with S
or D
or E
or F
or G
or H
, then things code works fine.
How do I understand the working of two scenarios from aforementioned points? Please help!!!