• 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

Can a top-level class extend a member class

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am getting compilation error, when trying to extend a top-level class from a member class.

class outer
{
class inner
{
}
}
class test extends outer.inner
{
}
Is this the right way to do it? Thanks in advance.
regards,
Usha
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An inner class cannot exist without a parent class being instantiated. So extending an inner class without its parent class is like accessing a feature of the parent without having the parent itself. I suppose that's the reason for the compilation error.
But I also have a question, how will you then extend an inner class even if u have an parent instantiated, instead you can extend the parent itself, right ?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was thinking that you should not extend an Inner class because. . ., well thats why it is an Inner and not a top lavel class, to be encapsulated in its outer class.
Its like want to inherit a method from a class that you dont want to extend.
The only way I've found is by declaring the inner class as static, that way the class exists before the Outer being created. After all a static inner class is considered as a top lavel class.


[This message has been edited by Zkr Ryz (edited May 11, 2001).]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An inner class can extend another class( either a top-level or member class ). For example, the following code compiles:

BTW, the words inner & outer are Java keywords.
[This message has been edited by js yang (edited May 11, 2001).]
 
Usha Vydyanathan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I got it. There is no problem in the extends clause. When the subclass ctor invokes superclass ctor you must specify the containing instance.
class test extends outer.inner
{
tesr(outer o)
{
o.super();
}
}
this works fine.
Thanks,
Usha
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the below example will give a clear insight to how it works for multiple nested inner classes
class D {
class dinner1{
class dinnerMost1{}
}
class dinner3{
class dinnerMost3{
class dinnerMMost33 {}
}
}
class dinner4 extends dinner1{}
class dinner5 extends dinner1.dinnerMost1{
dinner5(dinner1 d) {
d.super();
}
}
class dinner6 extends dinner3.dinnerMost3.dinnerMMost33 {
dinner6(dinner3.dinnerMost3 d)
{ d.super();}
}
}
reply
    Bookmark Topic Watch Topic
  • New Topic