• Post Reply Bookmark Topic Watch Topic
  • New Topic

inner class  RSS feed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can inner class (and both inner-interface and abstract inner class) be extended outside the outer class?
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By definition, an "inner class" is a nested class that is not static, so it's always tied to an instance of the enclosing class. Nested interfaces are implicitly static, so there's no such thing as an "inner interface."

As for "extending" an inner class outside the enclosing class... It can be done (if I'm understanding what you mean by "outside"), but you do need to create that enclosing instance. For example...
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[marc]: By definition, an "inner class" is a nested class that is not static, so it's always tied to an instance of the enclosing class.

Not always. For beginner-level readers: this may be a non-beginner-level digression, so please skip if it seems too complex. Just remember that an inner class does not always have access to an outer instance, and come back to this if/when you want more detail. So, for those who want an explanation of my "not always":

It's possible to have a local or anonymous class which is defined in a "static context" (e.g. within a static method), which is not technically a static class and therefore is an inner class, but which does not have access to any enclosing instance. Why is it important to know that a class can be an inner class even if it has no outer instance? Consider:

The "private static int x" won't compile, because Inner is an inner class, and therefore can't declare static members.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going back to the original post, I think it's likely that Arun really meant to say "nested" rather than "inner", so let's answer that question:

[Arun (modified)]: can a nested class (and both nested-interfaces and abstract nested classs) be extended outside the outer class?

Yes, absolutely, if they are static. (Which includes all nested interfaces.) You just have to include the outer class name when referring to the static nested class, e.g.

OuterClass.NestedClass

Other than that, there are no special rules for static nested classes that would prevent you from extending them. Well, assuming the class is not private, or protected or package-level and inside another package.

If the nested class is not static, that's more complex, and you should go back to Marc's first answer.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
...It's possible to have a local or anonymous class which is defined in a "static context" (e.g. within a static method), which is not technically a static class and therefore is an inner class, but which does not have access to any enclosing instance...

Hmmm... Wouldn't the static context make the class implicitly static?

(I'm considering JLS 8.1.3: "An inner class is a nested class that is not explicitly or implicitly declared static.")
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By definition all local and anonymous classes are inner classes. It doesn't say so explicitly with local classes but they're never static, otherwise they wouldn't be an inner class.

JLS �14.3

All local classes are inner classes (�8.1.3).



JLS �15.9.5

An anonymous class is always an inner class (�8.1.3); it is never static (�8.1.1, �8.5.2).

 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized why it doesn't say so explicitly: a local class can't be static to begin with.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also there's this from JLS 8.1.3: "An instance of an inner class I whose declaration occurs in a static context has no lexically enclosing instances."

It certainly sounds like a reasonable guess to say that a class declared in a static context is implicitly static, but the JLS never comes out and says this - and in fact the opposite is true. The context is static, but no one said the class was. An inner class declared in a static context is a funny hybrid of a "normal" inner class, and a static nested class, in the sense that all the rules given for inner classes apply, but there's no outer instance associated. Note that the JLS also never says that all inner classes have outer instances; that idea comes from other sources.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
I just realized why it doesn't say so explicitly: a local class can't be static to begin with.


Actually, come to think of it, neither can anonymous classes. Neither local nor anonymous classes can ever be implicitly or explicitly static. I'm not sure why the JLS makes it clear in one case and not in the other.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anonymous classes are local classes (by definition?) implicitly.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
...An inner class declared in a static context is a funny hybrid of a "normal" inner class, and a static nested class, in the sense that all the rules given for inner classes apply, but there's no outer instance associated...

Thanks Jim and Ken, that makes sense.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Tony]: Anonymous classes are local classes (by definition?) implicitly.

Not by the JLS definition, certainly.

JLS 14.3: "A local class is a nested class (�8) that is not a member of any class and that has a name."
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
Anonymous classes are local classes (by definition?) implicitly.


Are they?

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep your code sample is compiled to an initializer (static for your case) - which is a local context.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
[Tony]: Anonymous classes are local classes (by definition?) implicitly.

Not by the JLS definition, certainly.

JLS 14.3: "A local class is a nested class (�8) that is not a member of any class and that has a name."


We all know I - and other implementors - don't refer to the JLS as an authority (the contrary if anything) but whatever the case, I was just referring to the fact anonymous classes always exist in a local execution context.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!