File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes access violation with a protected 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 "access violation with a protected" Watch "access violation with a protected" New topic

access violation with a protected

Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298

According to the rule of the default constructor, Outer.Inner has a protected constructor like the following:
protected Inner(){super();}
but why there is an error if this constructor is accessed in a sub class of Inner???
thanks in advance!!!
Anshul Chhabra

Joined: Feb 18, 2003
Posts: 10
The class Inner does not have a constructor defined at all. Therefore, Java will provide a default constructor for Inner.
According to JLS - the modifier for the constructor will be the same as the modifier for the class (in this case protected).
Therefore the declaration of Inner is effectively:
protected class Inner{
protected Inner(){}

With this in mind it is pretty clear why SonOfOuter cannot access Inner, since the constructor of Inner is now protected.
To check, try defining a public constructor within Inner and see if your code compiles now.
Dan Culache
Ranch Hand

Joined: Jan 24, 2003
Posts: 70
The piece of code is from the Java Language Specification and the explanation is there 8.8.7 Default Constructor
The constructor for Inner is protected. However, the constructor is protected relative to Inner, while Inner is protected relative to Outer. So, Inner is accessible in SonOfOuter, since it is a subclass of Outer. Inner's constructor is not accessible in SonOfOuter, because the class SonOfOuter is not a subclass of Inner! Hence, even though Inner is accessible, its default constructor is not.
I agree. Here's the link:
subject: access violation with a protected
It's not a secret anymore!