This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to subclass a inner class ?

 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • 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 ?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jose & Ravish,
that was pretty coool. complex syntax, huh?
thanks!
Maulin.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic