• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Extending inner class.  RSS feed

 
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to extend an inner class. I know 2 ways but it's confusing :

1) within the same Outer class :


Why does Inner2 have to use Outer.Inner even though it should have access to Inner being in the same class ?

2) Outside Outer class


Why can't i use Outer.Inner with Second class ?? Why do i have to extend Outer First ??
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Inner Class is a member of the class it is enclosed in, in the same way that the fields of the enclosing class are members of that class. Instances of Inner each belong to a specific instance of Outer, and cannot be created without an Outer. That is why you can't extend Inner from a class that is not an Outer.



Output:
1
2


Notice how each instance of Inner prints out the value of the instance of Outer it was created from. This is because they are part of the enclosing object and can directly access all of its fields and methods, just like non-static methods in that class can. That is why you can only extend Inner from classes that are an Outer.

If we add the following static method to Outer it will fail to compile:

 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mike - I would suggest that while providing explanations don't go against the rules of the language such as using keywords for identifiers (do()) in the event that someone doesn't know better...

Hint: There is a logic error in your constructor for Outer

Valid explanation besides
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Rico, I will fix that. In fact I'm sure I did fix that, I guess it didnt save properly.

I'll also change the name of method.

Thanks
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why in the First example i have to use Outer.Inner to extend in the same class ?? It should be available within the same Outer class..

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you can as both will be members of the same class...

Try the following snippets as a test:


 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!