Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Inner classes, Inheritance and Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inner classes, Inheritance and Access" Watch "Inner classes, Inheritance and Access" New topic
Author

Inner classes, Inheritance and Access

Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
I have a question about the apparently inconsistent behavior of inner classes when in comes to inheritance and access control. According to Sierra and Bates,p464:

A regular inner class is a member of the outer class just as instance variables and methods are


With that in mind, could someone explain the following?:


If inner classes are supposed to behave like member variables and methods (at least in terms of access modifiers and inheritance), why can I directly refer to the inner class (above) from another class not in the same package? Should I not inherit the inner class definition, and then instantiate it through the sub class?
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
Actually I was using 'super' as a variable name there by mistake, and changing it to 'sup' does throw a visibility error on trying to instantiate. But it does not complain about the Super.Inner reference - why?
Bradley Smith
Greenhorn

Joined: Jun 21, 2004
Posts: 13
Originally posted by Fletcher Estes:
But it does not complain about the Super.Inner reference - why?


Because Sub derives form Super and thus has access to it's public AND protected members, just not the private ones. You can just as easily reference a protected static attribute e.g.

Perhaps I have misunderstood your question?
[ July 07, 2004: Message edited by: Bradley Smith ]
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
I think I have a better idea now. There is a difference between static accessiblity and instance accessibility. My main problem is, if the following code is compiled (given the original source I provided):

The compiler will not allow the above - the subclass does not have direct access to to the superclass's protected methods/variables, it merely inherits them. So you can instantiate a Sub, and call the inherited methods through that Sub reference:


With static references, however, the compiler seems to allow direct rather than inherited access to the superclass's methods and variables.
Bradley Smith
Greenhorn

Joined: Jun 21, 2004
Posts: 13
That's kind of right.
If you override method() then in your overridden "method" you can call:


no problem.

But if you've got some function somewhere that just creates an instance of the base class "Super" then that function doesn't have any special rights to access protected members of that class.

It's hard to explain why things appear to be different with statics but if you take a look at this code and then build and run it then it might makes sense

Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
The compiler hopefully enforces access control, but the runtime has no concept on controlling access to inner classes. It treats it as a top-level class.


Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes, Inheritance and Access
 
Similar Threads
java rules roundup game
default vs protected access
Inner classes, Inheritance and Access
Help in statics
Question regarding protected modifier