Java : Protected access restriction for subclass on superclass object

java

Solution

Protected members can only be accessed outside of the same package if it's via inheritance - i.e. within the hierarchy.

So when you're creating another instance of A from a different package, that's not an inheritance relationship and it thus fails.

As always, this is covered in the JLS, 6.6.2:

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

Problem

I know that this has been asked before in this forum but i will ask again since i don't see any good answer (so far). Here it goes: ``` package a; public class A{ protected int a; } package b; public class B extends A{ } package c; public class C extends B{ public void accessField(){ A ancient = new A(); ancient.a = 2; //A - That wouldn't work. a = 2; //B - That works. } } ``` Why clause A) won't work? What's the rational behind this restriction on superclass object ancient access in subclass C? Thanks.

Original source