As per my understanding rules and access level of protected variable :
If same package
class A {
protected int =200;
}
class B extends A {
B is ref variable of B
B.i overriding the value of i in class B
b.i = 400;
a is ref variable of A
a.i will always print 200
}
class C extends B {
c is ref variable of C c.i overriding the value of i in class C
c.i = 500;
a is ref variable of A a.i will always print 200
}
Conclusion : Protected member of class A is public in same package for all the classes.
if different packages use A
Here A and D is not in same package
class D extends A {
System.out.print(a.i);
Above statement will throw compile time error
Here a.i is not accessible, i is now private in A
You can use i only with ref variable of D
System.out.print(d.i);
//no error
}
class E extends D {
System.out.print(a.i);
Above statement will throw compile time error
Here a.i is not accessible, i is now private in A
System.out.print(d.i);
Above statement will throw compile time error
Here d.i is not accessible, i is now private in D
You can use i only with ref variable of E
System.out.print(e.i);
No error
}
Conclusion : Protected member of class A is behaving as private variable in other packages for its immediate sub class.
Can anyone give me more details on access level of protected variables ?