File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to subclass a inner class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to subclass a inner class ?" Watch "how to subclass a inner class ?" New topic
Author

how to subclass a inner class ?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Dec 18, 2001
Posts: 417
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

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
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:



"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Jose & Ravish,
that was pretty coool. complex syntax, huh?
thanks!
Maulin.
 
Consider Paul's rocket mass heater.
 
subject: how to subclass a inner class ?