• 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

how to subclass a inner class ?

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Base
{
static class Inner
{
public static void main(String[] args)
{
System.out.println("Inner");
}
}
}
public class MainTest extends Base.Inner
{
}
this code was posted and it is fine. Question here is in a different context.
Here the class Inner is actually a TopLevel class. IF this class were a pure inner class instead then how would we extend the MainTest class ?
surely "class MainTest extends Base.Inner" would not work. Then what would be correct way ?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi mark,
i don't know how to do what u want but i have another option.
one can subclass class Inner if we use anonymous inner classes. here is my example,

here output would be "Hello!".
any other pointers?
regards
maulin
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes that way is fine. but my intent of asking was to know how to refer to such a class. Because if the it was a TopLevel class then one can always refer it to like
MainTest extends Base.Inner
but what if the Inner class was a pure inner class. would be good to know this.

Originally posted by Maulin, Vasavada:
hi mark,
i don't know how to do what u want but i have another option.
one can subclass class Inner if we use anonymous inner classes. here is my example,

here output would be "Hello!".
any other pointers?
regards
maulin

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi mark,
but i guess when we say "inner class is a instance member" then it should mean that.
so, if we try to access by any other means except the object ref of the container class it must fail. which is essentially it does. so, i think there is nothing wrong if we can't access it for subclassing it.
i've not used any inner class so far except anonymous one. so, i really cant thro much light on this issue.
any other Java Gurus can help here???
regards
maulin.
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess you are right. we cannot have a subclass of such a inner class because inner class is an instance member of the container class. i guess you are correct. but let's ask some senior java guys here, just to make sure.

Originally posted by Maulin, Vasavada:
hi mark,
but i guess when we say "inner class is a instance member" then it should mean that.
so, if we try to access by any other means except the object ref of the container class it must fail. which is essentially it does. so, i think there is nothing wrong if we can't access it for subclassing it.
i've not used any inner class so far except anonymous one. so, i really cant thro much light on this issue.
any other Java Gurus can help here???
regards
maulin.

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java guru Bruce Eckel explains in his excellent Thinking in Java 2ed :
For creating an instance of the class that extends an inner class, an instance on the class that encloses the base one must be provided to the constructor of the derived class. This constructor must invoke the base constructor expelicitetly for being eble to pass it the instance of the enclosing class.

Test class inherits the method that is able to call the private method of the enclosing class.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from JLS:

Qualified superclass constructor invocations begin with a Primary expression. They allow a subclass constructor to explicitly specify the newly created object's immediately enclosing instance with respect to the direct superclass (�8.1.2). This may be necessary when the superclass is an inner class. Here is an example of a qualified superclass constructor invocation:

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jose & Ravish,
that was pretty coool. complex syntax, huh?
thanks!
Maulin.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic