Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > super.super.super how?

Reply
Thread Tools

super.super.super how?

 
 
Guest
Posts: n/a
 
      02-19-2005
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)
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      02-19-2005
> 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.
 
Reply With Quote
 
 
 
 
DP
Guest
Posts: n/a
 
      02-19-2005

"<- Chameleon ->" <(E-Mail Removed)> wrote in message
news:cv61hh$7dp$(E-Mail Removed)...
>> 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.


 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      02-19-2005
"DP" <(E-Mail Removed)> wrote in message
news:42168fe0$0$25735$(E-Mail Removed)...
>
> "<- Chameleon ->" <(E-Mail Removed)> wrote in message
> news:cv61hh$7dp$(E-Mail Removed)...
>>> 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?


 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      02-19-2005
"<- Chameleon ->" <(E-Mail Removed)> wrote in message
news:cv61hh$7dp$(E-Mail Removed)...
>> 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.


 
Reply With Quote
 
Darryl Pierce
Guest
Posts: n/a
 
      02-19-2005
<- 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 <(E-Mail Removed)>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard
 
Reply With Quote
 
JS
Guest
Posts: n/a
 
      02-20-2005
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" <(E-Mail Removed)> wrote in message
news:m3HRd.5170$(E-Mail Removed)...
> <- 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 <(E-Mail Removed)>
> Visit my homepage: http://mcpierce.multiply.com
> "By doubting we come to inquiry, through inquiry truth." - Peter Abelard



 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-20-2005
<- 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


 
Reply With Quote
 
Darryl Pierce
Guest
Posts: n/a
 
      02-20-2005
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 ****-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 <(E-Mail Removed)>
Visit my homepage: http://mcpierce.multiply.com
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard
 
Reply With Quote
 
JS
Guest
Posts: n/a
 
      02-20-2005
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" <(E-Mail Removed)> wrote in message
news:6Z0Sd.15962$(E-Mail Removed). ..
> 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 ****-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 <(E-Mail Removed)>
> Visit my homepage: http://mcpierce.multiply.com
> "By doubting we come to inquiry, through inquiry truth." - Peter Abelard



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments