Override an inner class?

D

daniel.w.gelder

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
 
T

Torkel Franzen

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".)
 
D

daniel.w.gelder

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
 
C

Chris Smith

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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Mike Schilling

Chris Smith said:
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.)


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.


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.
 
T

Torkel Franzen

Chris Smith said:
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.
 
C

Chris Smith

Mike Schilling said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Torkel Franzen said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Mike Schilling

Chris Smith said:
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.
 
C

Chris Smith

Mike Schilling said:
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.

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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
P

Piotr Kobzda

Chris said:
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
 
T

Torkel Franzen

Piotr Kobzda said:
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.
 
D

daniel.w.gelder

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?)
 
T

Torkel Franzen

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.
 
P

Piotr Kobzda

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.

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
 
M

Mike Schilling

Chris Smith said:
Right, but there are implications to InnerSub being in a different file.

Again, I thought the suggestion was to get around those implications.
 
C

Chris Smith

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

Learn something every day. Hmm.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top