Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Inner Interfaces vs Nested Interfaces

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Im confused on a couple of points here.
1. How can a nested class not be an inner class?
2. Rob indicated that nested interfaces are permissable, and they can have any access modifier
If there is a difference between nested and inner could someone please demonstrate the two with code? And if there is such a difference, is it suffice to say that nested interfaces are allowed, yet inner interfaces are not?
Thanks
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I can tell you the difference between a nested class, and an inner class. I haven't looked at the interface portion of this issue yet, but I believe (and someone can correct me if I am wrong) that you cannot have an inner-interface, only nested interfaces.
When you declare class or interface as a member of another class or interface, that class/interface is nested within the outer class/interface declaration.

Above, the bar class is nested within foo. Here's another example:

Here again, class bar2 is nested within class foo2.
I think this concept is pretty clear. But one more...

This is a nested interface.
Ok, so that's nesting.
Now, an inner class IS a nested class that has a reference to an instance of an outer class. That is, an inner class can only exist as an instance to an object of an outer class. You can't create an inner class without going through the outer class. Put another way, an inner class always has an implicit "this" reference to the enclosing class instance.
If I want to instantiate a bar class from my first example, I have to write new foo().new bar(). In other words, since bar is an inner class, I have to create it from an instance of the enclosing foo class.
In my second example, bar2 is a static nested class, meaning that I can have instances of it without having an instance of the enclosing object.
I can write
foo2.bar2 myNewObject = new foo2.bar2().
Compare this to the instance creation expression for the inner class above.
Finally, when dealing with interfaces, well, we don't instantiate interfaces, so there is no comparable example here. If I want to refer to interface bar3, I have to qualify it with the name of it's enclosing class..
foo3.bar3 anObjectThatImplementsTheBar3Interface;
That's what I know about nesting!
Rob
[ January 24, 2002: Message edited by: Rob Ross ]
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


an inner class IS a nested class that has a reference to an instance of an outer class. That is, an inner class can only exist as an instance to an object of an outer class. You can't create an inner class without going through the outer class. Put another way, an inner class always has an implicit "this" reference to the enclosing class instance.


Rob or someone do you have a code example, of what this will look like. Also doesnt a nested class requre an implicit reference to the enclosing class instance?
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The nested class examples were great btw
 
Ranch Hand
Posts: 5390
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Salerno:

Also doesnt a nested class requre an implicit reference to the enclosing class instance?


in hurry ...
Not nested but inner has implicit referrence to enclosing class instance.
CMIW
TIA
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Salerno:

Rob or someone do you have a code example, of what this will look like. Also doesnt a nested class requre an implicit reference to the enclosing class instance?


I gave you an example Paul. The first example is an inner class. You can say it's a nested class, because it's within another class, but it's also an inner class, because it has to be instantiated via the outer bar class.
It's a nested inner class. Compare to the second example, which is NOT an inner class, it's just a regular old nested class.

Rob
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,
Yes I believe it was just the inconsistencies of what is called inner class vs. nested class. Your code examples were great and I also found alot thru JLS. Example an inner class (which can also be called a nested class) isnt static, whereas a nested class is static.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remember that a nested class is just a class defined inside of a class or interface.
A nested interface is an interface that is declared inside of a class or interface.
An inner class is a special kind of nested class.

Rob
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The definitive answer from the JLS 8.1.2.

An inner class is a nested class that is not explicitly or implicitly declared static.


A nested class is, as already stated, a class declared inside of another class or interface. A nested interface is an interface declared inside another interface or class - since member interfaces are always implicitly static there is no distinction between different types (ie static and non-static).
hope that helps
 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome Explaination Rob!!
Keep it Up....
Sonir
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic