Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Override an inner class?

Thread view: 
daniel.w.gelder@gmail.com - 15 Jan 2006 09:02 GMT
Inner classes are real useful, especially if you do something like

SomeOtherClass soc = <something>;
SomeOtherClass.Inner o = soc.new Inner() { public void overrideStuff()
{} };

I like being able to override functionality of inner classes this way
when the inner class does something that really belongs to a particular
enclosing object. For example, SpriteWorld.Sprite.

But there seems to be no way to make a non-anonymous class that extends
another class's inner class. I've tried using the static keyword here
and there but it doesn't work.

It seems like this should be possible without messing up the integrity
of the object architecture.

TIA
Dan
Torkel Franzen - 15 Jan 2006 10:20 GMT
> But there seems to be no way to make a non-anonymous class that extends
> another class's inner class.

 Sure there is:

class A{
   class B{
   }
}

class C extends A{
   class E extends A.B{
   }
}

(Of course the declaration of E makes no sense without the "extends A".)
daniel.w.gelder@gmail.com - 15 Jan 2006 13:29 GMT
I know about that one...but it seems silly to override a SpriteWorld
and do nothing, just to subclass Sprite...and then I'll have to put all
my Sprite override code in one place.

I guess I'll have to make an interface that the Sprite calls to do its
work.

I think Java should have allowed
static class BetterSprite extends SpriteWorld.Sprite {

Since it wouldn't break mono-morphism or packages or anything. Or at
least let it be a first-level class.

Thanks
Dan
Chris Smith - 15 Jan 2006 16:37 GMT
> But there seems to be no way to make a non-anonymous class that extends
> another class's inner class.

That's correct (Torkel's example notwithstanding... in Torkel's cose, he
is overriding an inner class that's inherited into the class where he
extends it, so it's not really subclassing another class's inner class.)

> I've tried using the static keyword here
> and there but it doesn't work.

It should work if you make the inner class static... but then it's not
an inner class any more, so it's still not possible to subclass another
class's inner class except with an anonymous class.

> It seems like this should be possible without messing up the integrity
> of the object architecture.

Sure, it is theoretically possible.  However, it's not possible within
the Java language.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Mike Schilling - 15 Jan 2006 16:47 GMT
>> But there seems to be no way to make a non-anonymous class that extends
>> another class's inner class.
[quoted text clipped - 15 lines]
> Sure, it is theoretically possible.  However, it's not possible within
> the Java language.

Part of being an inner class is having access to the containing class's
private parts.  Allowing a subclass to have that same access would be a
security hole.  I don't (offhand) see any security issues so long as the
subclass doesn't have that special access.
Chris Smith - 15 Jan 2006 19:34 GMT
> Part of being an inner class is having access to the containing class's
> private parts.  Allowing a subclass to have that same access would be a
> security hole.  I don't (offhand) see any security issues so long as the
> subclass doesn't have that special access.

Sure.  Just as is the case with the anonymous inner class, the subclass
would have access to private members of its own enclosing lexical
scopes, but not the enclosing lexical scopes of the superclass.  
Subclasses in Java never have access to private members that are
accessible to their superclasses.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Torkel Franzen - 15 Jan 2006 17:17 GMT
> That's correct (Torkel's example notwithstanding... in Torkel's cose, he
> is overriding an inner class that's inherited into the class where he
> extends it, so it's not really subclassing another class's inner
> class.)

 True (reading "hiding" for "overriding"). But what would it mean to
subclass another class's inner class? You have to get a surrounding
instance from somewhere.
Chris Smith - 15 Jan 2006 19:36 GMT
> > That's correct (Torkel's example notwithstanding... in Torkel's cose, he
> > is overriding an inner class that's inherited into the class where he
> > extends it, so it's not really subclassing another class's inner
> > class.)
>
>   True (reading "hiding" for "overriding").

Actually, read "subclassing" for "overriding".  Sorry about that; my
mistake.

> But what would it mean to
> subclass another class's inner class? You have to get a surrounding
> instance from somewhere.

Yes.  The syntax would have to provide a means to specify separate
enclosing instances for the superclass and the subclass.  With anonymous
inner classes, there is such a mechanism: the enclosing instance for the
anonymous subclass is 'this', while the enclosing instance for the
superclass is specified as a qualifier for the "new" expression.  There
is no such obvious syntax for named inner classes, so I understand why
the language doesn't provide the ability.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Torkel Franzen - 15 Jan 2006 19:58 GMT
> Actually, read "subclassing" for "overriding".

 Yes, of course, the inherited inner class wasn't hidden in my
example.
Mike Schilling - 16 Jan 2006 01:45 GMT
> Yes.  The syntax would have to provide a means to specify separate
> enclosing instances for the superclass and the subclass.

Would it?  Perhaps I misunderstand the suggestion, but I thought it was to
be able to modify the behavior of an existing inner class, giving both the
same enclosing instance, much like

   class Outer
   {
       class InnerSuper
       {
       }

       class InnerSub extends InnerSuper
       {
       }
   }

but with InnerSub defined in a different file.
Chris Smith - 16 Jan 2006 05:40 GMT
> Would it?  Perhaps I misunderstand the suggestion, but I thought it was to
> be able to modify the behavior of an existing inner class, giving both the
[quoted text clipped - 12 lines]
>
> but with InnerSub defined in a different file.

Right, but there are implications to InnerSub being in a different file.  
It is, therefore, in a different top-level class... and assuming it's an
inner class (which was part of the original description), it must have
an enclosing instance of that top-level class.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Mike Schilling - 16 Jan 2006 18:03 GMT
>> Would it?  Perhaps I misunderstand the suggestion, but I thought it was
>> to
[quoted text clipped - 16 lines]
>
> Right, but there are implications to InnerSub being in a different file.

Again, I thought the suggestion was to get around those implications.
Piotr Kobzda - 16 Jan 2006 10:45 GMT
> There
> is no such obvious syntax for named inner classes, so I understand why
> the language doesn't provide the ability.

Java language does.

class A {
   class Inner {
   }
}

A a = new A();
A.Inner ai = a.new Inner() {};  // anonymous subclass of inner class

class AInnerSubclass extends A.Inner {  // non-anonymous one
   AInnerSubclass(A a) {
      a.super();
   }
}

ai = new AInnerSubclass(a);

Regards,
piotr
Torkel Franzen - 16 Jan 2006 10:59 GMT
> class AInnerSubclass extends A.Inner {  // non-anonymous one
>     AInnerSubclass(A a) {
>        a.super();
>     }
> }

 True, you can supply an enclosing instance in the constructor. This
simply didn't occur to me. It's rather remarkable that the compiler
can figure this out.
daniel.w.gelder@gmail.com - 16 Jan 2006 15:24 GMT
class A {
   class Inner {
   }

}

A a = new A();
A.Inner ai = a.new Inner() {};  // anonymous subclass of inner class
class AInnerSubclass extends A.Inner {  // non-anonymous one
   AInnerSubclass(A a) {
      a.super();
   }

}

This simply doesn't work. The compiler generates an error for the
reason that A.Inner is not accessible here; there is no enclosing
instance. And there isn't. (Which A is AInnerSubclass going to refer to
when it asks for A.this?)
Torkel Franzen - 16 Jan 2006 15:51 GMT
> This simply doesn't work.

 This compiles just fine:

class A{
   class B{
   }
}

class C extends A.B{
   C(A a){
    a.super();
   }
}

The interesting thing is that the compiler actually ponders the body
of the constructor in C to make sure that an enclosing instance is
provided.
Piotr Kobzda - 16 Jan 2006 17:06 GMT
> class A {
>     class Inner {
[quoted text clipped - 12 lines]
>
> This simply doesn't work.

It does, simply... copy and paste my example into main() method, then
show compilation errors here (if any).

> The compiler generates an error for the
> reason that A.Inner is not accessible here; there is no enclosing
> instance. And there isn't.

My example is very simple and it will not solve all your problems...
sorry. You have to follow other Java "rules", even in this case...

> (Which A is AInnerSubclass going to refer to
> when it asks for A.this?)

None. In my example A is not the enclosing class of AInnerSubclass, than
A.this is simply not allowed. You can access the enclosing instance of
type A referring it as final variable or storing (e.g. as field) the
instance of A passed to AInnerSubclass constructor.

A.this would be allowed in the context of AInnerSubclass only if there
were class A enclosing it.

BTW, this is quite interesting feature of Java allowing single instance
of inner class to have many instances of enclosing classes. Of course,
you can simply forget about it, if you don't need this feature... '-)

Regards,
piotr
Chris Smith - 16 Jan 2006 18:19 GMT
> class AInnerSubclass extends A.Inner {  // non-anonymous one
>     AInnerSubclass(A a) {
>        a.super();
>     }
> }

Learn something every day.  Hmm.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Roedy Green - 16 Jan 2006 19:48 GMT
>But there seems to be no way to make a non-anonymous class that extends
>another class's inner class. I've tried using the static keyword here
>and there but it doesn't work.

what happens when you redefine inner classes of inner classes?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.