wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected inner class problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected inner class problem" Watch "protected inner class problem" New topic
Author

protected inner class problem

Alex Iordanoglou
Greenhorn

Joined: Oct 21, 2004
Posts: 6
Hello everybody,

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...

Rachana Sharma
Greenhorn

Joined: Jun 21, 2005
Posts: 15
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.
Alex Iordanoglou
Greenhorn

Joined: Oct 21, 2004
Posts: 6
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:


or

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)


Anyway,.. thanks for the help !!!
 
Don't get me started about those stupid light bulbs.
 
subject: protected inner class problem