• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner class inheritance

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are the rules regarding inner class inheritance ??? can they be inherited or not ??

e.g.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can extend an inner class in any class. You just need an instance of its enclosing class to call its constructor
As for your question whether you can change code inside inner without extending it, you can try it and see what happens...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:You can extend an inner class in any class.




Agreed. Even if the sub class is an inner class in a different (and unrelated) outer class. The only restriction is that it must be accessible.

Henry
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey ankit....!!!

thanks for this code example.... i had tried it before but it was giving me error so that i thought that we cannot inherit inner class in a top level class but i just tested you code and it compiled fine...

you have put a constructor which instantiates outer class instance....

but i can't understand what exactly being done here .....

we are inheriting inner class.....

so simply "new Outer()" in constructor why is not enough??? what does "new Outer().super()" mean ???

would you please clear my doubt ???
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankur trapasiya wrote:
but i can't understand what exactly being done here .....

we are inheriting inner class.....

so simply "new Outer()" in constructor why is not enough??? what does "new Outer().super()" mean ???

would you please clear my doubt ???



The thing to remember here is that all inner classes depend on a outer class (assuming that the inner class is not a nested (aka static) class). This means that it is not possible to instantiate an inner class without an outer class.

This is not a legal statement...



You need an outer class...




This is also true when a subclass calls the constructor of its super class -- it needs an instance of the super class outer class. The exception is if the subclass is also an inner class that has the same outer class (or a subclass of the same outer class), then it will work, as it can use the sub class outer class.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also note that the example is not really ideal. It always creates a new outer class to instantiate the inner class. It is more flexible to have a constructor to create an inner class using any outer class -- and not requiring that a new one created.



Henry
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. class Outer {
2. class Inner {}
3. }
4. class Normal extends Outer.Inner {
5. Normal() {
6. new Outer().super(); // here new Outer() only why can't work ???
7. }
8. }
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankur trapasiya wrote:
// here new Outer() only why can't work ???




Remember that the first line of a constructor should be a call to the super constructor (or another constructor). If it isn't, the compiler will insert a call to the default super constructor... so this...



becomes this....




Which of course, won't work because the call to the super constructor will not have access to an instance of the outer class.

Henry
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic