super.super.super how?

Discussion in 'Java' started by Guest, Feb 19, 2005.

  1. Guest

    Guest Guest

    Whow! What a problem!


    class A
    class B extends A
    class C extends B

    How can I access "A" members from class "C"?

    super.super.a_base_class_member();
    is not working...


    I believe its too much, if I cannot have such access...
    I think, I have miss something here. (In C++ its too easy)
    Guest, Feb 19, 2005
    #1
    1. Advertising

  2. Guest

    Guest Guest

    > Whow! What a problem!
    >
    >
    > class A
    > class B extends A
    > class C extends B
    >
    > How can I access "A" members from class "C"?
    >
    > super.super.a_base_class_member();
    > is not working...
    >
    >
    > I believe its too much, if I cannot have such access...
    > I think, I have miss something here. (In C++ its too easy)


    I forgot!

    a_base_class_member()
    is implemented in both "A", "B", "C" classes.
    Guest, Feb 19, 2005
    #2
    1. Advertising

  3. Guest

    DP Guest

    "<- Chameleon ->" <> wrote in message
    news:cv61hh$7dp$...
    >> Whow! What a problem!
    >>
    >>
    >> class A
    >> class B extends A
    >> class C extends B
    >>
    >> How can I access "A" members from class "C"?
    >>
    >> super.super.a_base_class_member();
    >> is not working...
    >>
    >>
    >> I believe its too much, if I cannot have such access...
    >> I think, I have miss something here. (In C++ its too easy)

    >
    > I forgot!
    >
    > a_base_class_member()
    > is implemented in both "A", "B", "C" classes.


    unless a_base_class_member() is private, then forget it.
    DP, Feb 19, 2005
    #3
  4. Guest

    Ryan Stewart Guest

    "DP" <> wrote in message
    news:42168fe0$0$25735$...
    >
    > "<- Chameleon ->" <> wrote in message
    > news:cv61hh$7dp$...
    >>> Whow! What a problem!
    >>>
    >>>
    >>> class A
    >>> class B extends A
    >>> class C extends B
    >>>
    >>> How can I access "A" members from class "C"?
    >>>
    >>> super.super.a_base_class_member();
    >>> is not working...
    >>>
    >>>
    >>> I believe its too much, if I cannot have such access...
    >>> I think, I have miss something here. (In C++ its too easy)

    >>
    >> I forgot!
    >>
    >> a_base_class_member()
    >> is implemented in both "A", "B", "C" classes.

    >
    > unless a_base_class_member() is private, then forget it.
    >

    How would the method's being private help?
    Ryan Stewart, Feb 19, 2005
    #4
  5. Guest

    Ryan Stewart Guest

    "<- Chameleon ->" <> wrote in message
    news:cv61hh$7dp$...
    >> Whow! What a problem!
    >>
    >>
    >> class A
    >> class B extends A
    >> class C extends B
    >>
    >> How can I access "A" members from class "C"?
    >>
    >> super.super.a_base_class_member();
    >> is not working...
    >>
    >>
    >> I believe its too much, if I cannot have such access...
    >> I think, I have miss something here. (In C++ its too easy)

    >
    > I forgot!
    >
    > a_base_class_member()
    > is implemented in both "A", "B", "C" classes.


    What you're trying to do can't be done. "super" is a special reference to the
    super class, just as "this" is a reference to this class. There is no such
    member as "super.super". Consider redesigning your hierarchy. For instance,
    since every class overrides the method you're trying to call, perhaps each
    method should call super.method(). That would ultimately cause C.method to
    include A.method.
    Ryan Stewart, Feb 19, 2005
    #5
  6. <- Chameleon -> wrote:
    > Whow! What a problem!
    >
    >
    > class A
    > class B extends A
    > class C extends B
    >
    > How can I access "A" members from class "C"?
    >
    > super.super.a_base_class_member();
    > is not working...
    >
    >
    > I believe its too much, if I cannot have such access...
    > I think, I have miss something here. (In C++ its too easy)


    Can't be done. super() is a reference to the parent class. What you need
    to do is reconsider your design: if C extends B but needs to bypass B to
    get to A's functionality, then perhaps C is not really an instance of a
    B and therefore shouldn't be extending B at all. Try having C extend A
    directly.


    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
    Darryl Pierce, Feb 19, 2005
    #6
  7. Guest

    JS Guest

    What would happen if you called super from C, which is class B, the have a
    method in B which accesses super A. So a method in B is like a messenger
    sort of thing, just taking and passing parameters from C to A, and any
    results get passed back via B to C
    "Darryl Pierce" <> wrote in message
    news:m3HRd.5170$...
    > <- Chameleon -> wrote:
    > > Whow! What a problem!
    > >
    > >
    > > class A
    > > class B extends A
    > > class C extends B
    > >
    > > How can I access "A" members from class "C"?
    > >
    > > super.super.a_base_class_member();
    > > is not working...
    > >
    > >
    > > I believe its too much, if I cannot have such access...
    > > I think, I have miss something here. (In C++ its too easy)

    >
    > Can't be done. super() is a reference to the parent class. What you need
    > to do is reconsider your design: if C extends B but needs to bypass B to
    > get to A's functionality, then perhaps C is not really an instance of a
    > B and therefore shouldn't be extending B at all. Try having C extend A
    > directly.
    >
    >
    > --
    > Darryl L. Pierce <>
    > Visit my homepage: http://mcpierce.multiply.com
    > "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
    JS, Feb 20, 2005
    #7
  8. Guest

    Chris Uppal Guest

    <- Chameleon -> wrote:

    > How can I access "A" members from class "C"?


    As others have said: You can't do it. There is no legal bytecode sequence
    that will call the super of the super of a method.

    Actually you can do it via JNI, and so I suppose that any JVM implementation
    that used JNI (or internal equivalents) to implement reflection
    (java.lang.reflect.Method) would also be able to "get at" the super.super.
    However, I believe that current Sun implementations use a neat trick of
    generating (internally) the byetcode for a special class that forwards to the
    method, so I don't suppose that reflection would work either (but I haven't
    tried it).

    Of course, you can always do it by ensuring that the first subclass that
    overrides the root method, also provides a an extra method that forwards to
    just the root method

    class A { aMethod() {...} }
    class B { aMethod() {...} backDoorToAMethod() {super.aMethod() }
    class C { aMethod() {...} }

    but I think that it would be much easier to fix your design...

    -- chris
    Chris Uppal, Feb 20, 2005
    #8
  9. JS wrote:
    > What would happen if you called super from C, which is class B, the have a
    > method in B which accesses super A. So a method in B is like a messenger
    > sort of thing, just taking and passing parameters from C to A, and any
    > results get passed back via B to C


    You would have a piss-poor design is what you'd have, one that has
    explicitly bound itself to a particular inheritance chain. For starters,
    what would you declare as the return type for your method "super" and
    how will you handle the fact that you can't override a method and change
    the return type at the same time? Then, once you've overcome that
    language hurdle, you'd have to figure out how to get the reference to
    the "parent" (since it's not a separate object but a different way of
    looking at the current object) and then return it. The "super" keyword
    can only be used as a method call ("super()") in a constructor, and when
    used in a method it must be used in a method call and not as an entity
    itself...

    IOW, it's not possible to do what you're suggesting.

    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
    Darryl Pierce, Feb 20, 2005
    #9
  10. Guest

    JS Guest

    It can be done, ive just written a simple method and class structure
    following what has been said. the method B is called by C and calls A, its
    return type is the same as method A. Say its a String that is passed.

    public String A()
    {
    String s = "hello";
    return s;
    }

    public String B()
    {
    String str = super.A();
    return str;
    }

    public String C()
    {
    String s = super.B();
    return s;
    }

    It may not be 100% fantastic style but it works. Sorry about the poor sytanx
    of the above but I'm still pretty new to Java.
    "Darryl Pierce" <> wrote in message
    news:6Z0Sd.15962$...
    > JS wrote:
    > > What would happen if you called super from C, which is class B, the have

    a
    > > method in B which accesses super A. So a method in B is like a messenger
    > > sort of thing, just taking and passing parameters from C to A, and any
    > > results get passed back via B to C

    >
    > You would have a piss-poor design is what you'd have, one that has
    > explicitly bound itself to a particular inheritance chain. For starters,
    > what would you declare as the return type for your method "super" and
    > how will you handle the fact that you can't override a method and change
    > the return type at the same time? Then, once you've overcome that
    > language hurdle, you'd have to figure out how to get the reference to
    > the "parent" (since it's not a separate object but a different way of
    > looking at the current object) and then return it. The "super" keyword
    > can only be used as a method call ("super()") in a constructor, and when
    > used in a method it must be used in a method call and not as an entity
    > itself...
    >
    > IOW, it's not possible to do what you're suggesting.
    >
    > --
    > Darryl L. Pierce <>
    > Visit my homepage: http://mcpierce.multiply.com
    > "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
    JS, Feb 20, 2005
    #10
  11. Guest

    Guest Guest

    <- Chameleon -> wrote:

    >> Whow! What a problem!
    >>
    >>
    >> class A
    >> class B extends A
    >> class C extends B
    >>
    >> How can I access "A" members from class "C"?
    >>
    >> super.super.a_base_class_member();
    >> is not working...
    >>
    >>
    >> I believe its too much, if I cannot have such access...
    >> I think, I have miss something here. (In C++ its too easy)

    >
    >
    > I forgot!
    >
    > a_base_class_member()
    > is implemented in both "A", "B", "C" classes.


    hmmm.....
    I dont try but maybe it works?

    public class C extends B {
    public void a_base_class_member() {
    ((A) this).a_base_class_member();
    }
    .................
    }
    Guest, Feb 20, 2005
    #11
  12. Guest

    Alan Krueger Guest

    <- Chameleon -> wrote:
    > hmmm.....
    > I dont try but maybe it works?
    >
    > public class C extends B {
    > public void a_base_class_member() {
    > ((A) this).a_base_class_member();
    > }
    > .................
    > }


    I don't think this does what you want.
    Alan Krueger, Feb 20, 2005
    #12
  13. JS wrote:
    > It can be done, ive just written a simple method and class structure
    > following what has been said. the method B is called by C and calls A, its
    > return type is the same as method A. Say its a String that is passed.
    >
    > public String A()
    > {
    > String s = "hello";
    > return s;
    > }
    >
    > public String B()
    > {
    > String str = super.A();
    > return str;
    > }
    >
    > public String C()
    > {
    > String s = super.B();
    > return s;
    > }
    >
    > It may not be 100% fantastic style but it works. Sorry about the poor sytanx
    > of the above but I'm still pretty new to Java.


    The code above is pointless. I guess my problem is that I don't see a
    point to all of this. If B's just going to return A's value then why is
    it overriding the method in the first place? The question asked
    originally was for C to access A directly, and the answer is a
    resounding "no". The above code doesn't invalidate that answer...

    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
    Darryl Pierce, Feb 21, 2005
    #13
  14. Guest

    marcus Guest

    After all those resounding "no!"'s . . .
    can't he create a new object inside c like
    object b = (object b) this;
    ugly, I ageee, but won't that give him access to b's super?


    <- Chameleon -> wrote:
    > Whow! What a problem!
    >
    >
    > class A
    > class B extends A
    > class C extends B
    >
    > How can I access "A" members from class "C"?
    >
    > super.super.a_base_class_member();
    > is not working...
    >
    >
    > I believe its too much, if I cannot have such access...
    > I think, I have miss something here. (In C++ its too easy)
    marcus, Feb 21, 2005
    #14
  15. marcus <> scribbled the following:
    > After all those resounding "no!"'s . . .
    > can't he create a new object inside c like
    > object b = (object b) this;
    > ugly, I ageee, but won't that give him access to b's super?


    No. All it does is change the type of the reference. What the object is
    and how it works won't be affected at all.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "Make money fast! Don't feed it!"
    - Anon
    Joona I Palaste, Feb 21, 2005
    #15
  16. Guest

    marcus Guest

    Ok, of course
    So take it a step further and say
    objectB oB = new objectB(oC);
    and have a objectB constructer that flings a B type object for a given C
    type

    Joona I Palaste wrote:
    > marcus <> scribbled the following:
    >
    >>After all those resounding "no!"'s . . .
    >>can't he create a new object inside c like
    >>object b = (object b) this;
    >>ugly, I ageee, but won't that give him access to b's super?

    >
    >
    > No. All it does is change the type of the reference. What the object is
    > and how it works won't be affected at all.
    >
    marcus, Feb 21, 2005
    #16
  17. Guest

    Tilman Bohn Guest

    In message <i87Sd.327$>,
    JS wrote on Sun, 20 Feb 2005 21:18:06 GMT:

    [...]
    > public String A()
    > {
    > String s = "hello";
    > return s;
    > }
    >
    > public String B()
    > {
    > String str = super.A();
    > return str;
    > }
    >
    > public String C()
    > {
    > String s = super.B();
    > return s;
    > }

    [...]

    How is this relevant to the discussion so far? I thought you're talking
    about C extends B extends A? Here you just have three (poorly named) methods
    of the same class.

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 21, 2005
    #17
  18. Guest

    Tilman Bohn Guest

    In message <cvb4e0$mge$>,
    <- Chameleon -> wrote on Mon, 21 Feb 2005 00:59:59 +0200:

    [...]
    > hmmm.....
    > I dont try but maybe it works?
    >
    > public class C extends B {
    > public void a_base_class_member() {
    > ((A) this).a_base_class_member();
    > }
    > .................
    > }


    No need to try, this is simply not how polymorphism works. The object
    referenced by `this' in the above code _is_ a C, which no amount of
    casting the variable will change. Always remember: Casting does _not_ do
    anything at all to the object itself. However, you can access fields on A
    that are shadowed by fields on C that way (if they're visible from C),
    because field access is not polymorphic.

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
    Tilman Bohn, Feb 21, 2005
    #18
  19. marcus wrote:
    > After all those resounding "no!"'s . . .
    > can't he create a new object inside c like
    > object b = (object b) this;
    > ugly, I ageee, but won't that give him access to b's super?


    No, since the object referenced by the local variable b and the object
    itself are the *same object*. And, you can't access another object's
    super, so again the whole exercise is pointless. It *can't* be done.

    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
    Darryl Pierce, Feb 21, 2005
    #19
  20. marcus wrote:
    > Ok, of course
    > So take it a step further and say
    > objectB oB = new objectB(oC);
    > and have a objectB constructer that flings a B type object for a given C
    > type


    And you've now created a throwaway object to overcome an apparent flaw
    in a class hierarchy. This is just getting worse and worse...

    --
    Darryl L. Pierce <>
    Visit my homepage: http://mcpierce.multiply.com
    "By doubting we come to inquiry, through inquiry truth." - Peter Abelard
    Darryl Pierce, Feb 21, 2005
    #20
    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. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    709
    Bob Willan
    Nov 22, 2003
  2. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    471
    Michele Simionato
    Jan 15, 2004
  3. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  4. GOOD LUCK GROUP
    Replies:
    0
    Views:
    388
    GOOD LUCK GROUP
    Apr 28, 2008
  5. zweibieren

    A need for super.super.method()

    zweibieren, May 7, 2009, in forum: Java
    Replies:
    0
    Views:
    442
    zweibieren
    May 7, 2009
Loading...

Share This Page