super.super.super how?

G

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)
 
G

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

Ryan Stewart

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

Darryl Pierce

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

JS

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
 
C

Chris Uppal

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
 
D

Darryl Pierce

JS said:
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.
 
J

JS

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

Guest

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();
}
.................
}
 
A

Alan Krueger

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

Darryl Pierce

JS said:
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...
 
M

marcus

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?
 
J

Joona I Palaste

marcus said:
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.
 
M

marcus

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
 
T

Tilman Bohn

In message <[email protected]>,
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.
 
T

Tilman Bohn

In message <[email protected]>,
<- 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.
 
D

Darryl Pierce

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

Darryl Pierce

marcus said:
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...
 

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

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top