• 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 Classes Extension

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please advise how to extend inner classes. The foll gives compile errors.

Regards JPraveen


(Added [C0DE][/C0DE] tags)
[ October 05, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Praveen,

Watch out for //NOTE comment lines:


Regards

Ankur
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What compiler errors?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code gives only one error "Outer.java:5: an enclosing instance that contains Outer.Inner is required"

As we are trying to extend Inner classes outside the enclosing class, so we can extend any inner class which is static but we can not exten an inner class which is not static.

Kaps
 
JPraveen Kumar
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ankur,
Your solution worked but please explain the code also.
Why do we need to expilictly call the super () of the paramter object as it has been already initialized while calling the function (i.e. inside main -> new Outer()).

Secondly where would the default super constructor call be placed by the compiler before out.super() or after out.super(). I tried inserting both but neither worked. Isnt this a violation of the rule that frst line of the constructor should be this() or super()?

Thirdly if the innerclass has a non default constructor then how would we get this working? Like

class Outer {
class Inner {
Inner (int i){}
}
}

Thx JPraveen
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the extensions slightly to avoid confusions with the explanation


class InheritInner extends Outer.Inner {}
class InheritInnerk extends Outer.k {}



The default constructor inside InheritInner cant call the superclass directly because a non static inner class is being extended(cant call super without an instance of the outerclass). A non static inner class is always associated with an instance of the outerclass, therefore you need to pass an instance of the outer class to the InheritInner constructor. This is not the case for the InheritInnerk class, because k is static inner class there can be an instance of k without an instance of the outer class.


Here is an example that I tested



The output is
outer constructor
inner constructor
static constructor

hope the explanation is clear.
[ October 06, 2004: Message edited by: Inuka Vincit ]
 
Bharat Roy
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Praveen,

Why do we need to explicitly call the super () of the parameter object as it has been already initialized while calling the function


Am I calling the Outer constructor inside ExtendInner's constructor??? Think again.

Isnt this a violation of the rule that frst line of the constructor should be this() or super()?


First line *is* an explicit call to the super class's constructor.

Thirdly if the innerclass has a non default constructor then how would we get this working?


It will *not* work! There must be default constructor present in the inner class.

Regards

Ankur
reply
    Bookmark Topic Watch Topic
  • New Topic