This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Since protected generally means package plus kids, why doesn't a protected inner class gets inherited by a subclass outside of the package, like instance variables and methods do? At least this seems to be happening below... Am I missing something or doing something wrong? Any help most welcome...
hi Alex first of all ur question gave me a chance to learn more about the protected access modifier.
what i could find is that,though we declare the inner class as protected when we actually extend the outer class in a differnt package in your case the "kidout extends dad" as we don't have any constructor for the nested class, so when we try to create it's object through the outer class then a default constructor comes into play.
this default constructor has the visibilty of protected as that of its class(the inner class) ans since we dont extend the inner class directly we can't inherit the constructor.
so, the solution to the problem is just create a constructor with public visiblity in the inner class. hope this helps....... as iam just a beginner.
Joined: Oct 21, 2004
Thanks Rachana!.. if "beginners" are like you maybe I should give up.. Indeed, now it seems we have two solutions:
1. We can extend the inner class, and use an instance of that instead, like below:
2. We can add a public constructor to the protected inner class in class Dad. In this case we can say (from the child outside the package)
The only weird thing is that, in this case, we can also say (again, from the child outside the package)