Override an inner class?

Discussion in 'Java' started by daniel.w.gelder@gmail.com, Jan 15, 2006.

  1. Guest

    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
     
    , Jan 15, 2006
    #1
    1. Advertising

  2. writes:

    > 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".)
     
    Torkel Franzen, Jan 15, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , Jan 15, 2006
    #3
  4. Chris Smith Guest

    <> wrote:
    > 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
     
    Chris Smith, Jan 15, 2006
    #4
  5. "Chris Smith" <> wrote in message
    news:...
    > <> wrote:
    >> 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.


    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.
     
    Mike Schilling, Jan 15, 2006
    #5
  6. Chris Smith <> writes:

    > 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.
     
    Torkel Franzen, Jan 15, 2006
    #6
  7. Chris Smith Guest

    Mike Schilling <> wrote:
    > 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
     
    Chris Smith, Jan 15, 2006
    #7
  8. Chris Smith Guest

    Torkel Franzen <> wrote:
    > Chris Smith <> writes:
    >
    > > 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.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jan 15, 2006
    #8
  9. Chris Smith <> writes:

    > Actually, read "subclassing" for "overriding".


    Yes, of course, the inherited inner class wasn't hidden in my
    example.
     
    Torkel Franzen, Jan 15, 2006
    #9
  10. "Chris Smith" <> wrote in message
    news:...
    >
    > 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.
     
    Mike Schilling, Jan 16, 2006
    #10
  11. Chris Smith Guest

    Mike Schilling <> wrote:
    > 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
     
    Chris Smith, Jan 16, 2006
    #11
  12. Piotr Kobzda Guest

    Chris Smith wrote:

    > 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
     
    Piotr Kobzda, Jan 16, 2006
    #12
  13. Piotr Kobzda <> writes:

    > 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.
     
    Torkel Franzen, Jan 16, 2006
    #13
  14. Guest

    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?)
     
    , Jan 16, 2006
    #14
  15. writes:

    > 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.
     
    Torkel Franzen, Jan 16, 2006
    #15
  16. Piotr Kobzda Guest

    wrote:

    > 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
     
    Piotr Kobzda, Jan 16, 2006
    #16
  17. "Chris Smith" <> wrote in message
    news:...
    > Mike Schilling <> wrote:
    >> 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.


    Again, I thought the suggestion was to get around those implications.
     
    Mike Schilling, Jan 16, 2006
    #17
  18. Chris Smith Guest

    Piotr Kobzda <> wrote:
    > 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
     
    Chris Smith, Jan 16, 2006
    #18
  19. Roedy Green Guest

    On 15 Jan 2006 01:02:50 -0800, wrote, quoted
    or indirectly quoted someone who said :

    >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?
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 16, 2006
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,937
    Thomas Weidenfeller
    Oct 12, 2005
  2. Carlo v. Dango
    Replies:
    14
    Views:
    1,103
    Alex Martelli
    Oct 19, 2003
  3. Pyenos
    Replies:
    2
    Views:
    415
    Pyenos
    Dec 27, 2006
  4. Ralf
    Replies:
    2
    Views:
    1,854
    Steve Wolter
    Mar 12, 2009
  5. Jer A
    Replies:
    2
    Views:
    185
    Dan Zwell
    May 24, 2007
Loading...

Share This Page