🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.java.programmer
7 messages
7 total messages Started by Carsten Friedrich Tue, 01 May 2001 00:52
calling method of super class of super class
#171
Author: Carsten Friedrich
Date: Tue, 01 May 2001 00:52
25 lines
1707 bytes
Hi,

is it possible to call a method of the super class of the super class?
E.g. (Pseudo java):

Class A { void f() { print "a";}}

Class B extends A { void f() {print "b"};}

Class C extends B { void f() {  A.f();}}

unfortunately Java does not allow me to do it like that (unless f is
static). Is there another way?

I can imagine some workarounds which all have some flaws:
 - reimplement the method from A , problem: the implementation of
   A.f() might not be known to me.

 - Have a class variable 'a' of type 'A' and call 'a.f()', which     
   wastes memory and does not always work ( A might be abstract)
 and so on ....

Is there a good solution?

Carsten
Re: calling method of super class of super class
#172
Author: Babu Kalakrishnan
Date: Tue, 01 May 2001 04:42
48 lines
2659 bytes
On Tue, 01 May 2001 00:52:20 GMT, Carsten Friedrich <Carsten.Friedrich@gmx.de> wrote:

>is it possible to call a method of the super class of the super class?
>E.g. (Pseudo java):
>
>Class A { void f() { print "a";}}
>
>Class B extends A { void f() {print "b"};}
>
>Class C extends B { void f() {  A.f();}}
>
>unfortunately Java does not allow me to do it like that (unless f is
>static). Is there another way?

As far as C is concerned, it extends B - and hence inherits only B's
properties. If the method f() has been over-ridden by B, you would
expect to be able to access only B's implementation of f() isn't it ?
Any property that C inherits from A would be only those that B doesn't
override - which makes perfect sense to me.

In case class B wants to provide access to A's method f() to its
descendants and at the same time have its own implementation of f(), I
guess it would need to implement it as another method - say something like :

void ancestorF()
{
    super.f();
}

>I can imagine some workarounds which all have some flaws:
> - reimplement the method from A , problem: the implementation of
>   A.f() might not be known to me.

The reason for needing access to A's implementation of the method isn't
very clear. If you needed that, you'd extend A and not B.

> - Have a class variable 'a' of type 'A' and call 'a.f()', which     
>   wastes memory and does not always work ( A might be abstract)
> and so on ....

And also depending on the implementation details (which is probably
unknown to you), calling the method on a different instance than your
own is unlikely to do the same thing anyway. (For instance if method f()
changed the value of an instance variable x, it would be a.x that
changes, not this.x)


BK
Re: calling method of super class of super class
#173
Author: Tor Iver Wilhelmsen
Date: Tue, 01 May 2001 09:13
18 lines
1739 bytes
Carsten.Friedrich@gmx.de (Carsten Friedrich) writes:

> is it possible to call a method of the super class of the super
> class?

No. Basically, a class knows its super class, but not that class'
superclass. If you end up needing to call a method two levels down you
need to reconsider your design.

> Is there a good solution?

Expose the underlying base implementation as a separate final method.

-- 
  Tor Iver Wilhelmsen                              <toriw@chello.no>
We are Oemgee of Boochrg. Your design, as you have known it, is over. Your
notational and conceptual uniqueness will be added to our own. Resistance is
not a valid Use Case. You will be assUMLated.
Re: calling method of super class of super class
#174
Author: Carsten Friedrich
Date: Wed, 02 May 2001 03:45
18 lines
1764 bytes
>Carsten.Friedrich@gmx.de (Carsten Friedrich) writes:
>
>> is it possible to call a method of the super class of the super
>> class?
>
>No. Basically, a class knows its super class, but not that class'
>superclass. If you end up needing to call a method two levels down you
>need to reconsider your design.
>

Thanks for the answer. Re-designing is not so easy in this case.
Basically I extend a 3rd party class with a clever hash-function.
Unfortunately my sub-class has the effect that the hash-funtion
degenerates. So I'd like to use the hash-function as it is implemented
in Object.

Carsten
Re: calling method of super class of super class
#175
Author: Lee Fesperman
Date: Wed, 02 May 2001 05:06
19 lines
2100 bytes
Carsten Friedrich wrote:
> 
> Thanks for the answer. Re-designing is not so easy in this case.
> Basically I extend a 3rd party class with a clever hash-function.
> Unfortunately my sub-class has the effect that the hash-funtion
> degenerates. So I'd like to use the hash-function as it is implemented
> in Object.

Then using the hash function from Object is not the solution, because it matches the 
implementation of equals() in Object. Your hashCode() must not break its contract with 
equals(). Implement one that matches the equals() in your sub-class. Just try to do a 
good job in your hashCode() override; the api will randomize your hash before using it 
anyway (by dividing by a prime or near-prime).

-- 
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
===================================================================
* Check out Database Debunkings (http://www.firstsql.com/dbdebunk/)
* "The Forum Where Database Matters Are Set Straight"
Re: calling method of super class of super class
#176
Author: Mats Olsson
Date: Wed, 02 May 2001 09:15
22 lines
1820 bytes
In article <3aef81b7.5615427@news.usyd.edu.au>,
Carsten Friedrich <Carsten.Friedrich@gmx.de> wrote:
>
>>Carsten.Friedrich@gmx.de (Carsten Friedrich) writes:
>>
>>> is it possible to call a method of the super class of the super
>>> class?
>>
>>No. Basically, a class knows its super class, but not that class'
>>superclass. If you end up needing to call a method two levels down you
>>need to reconsider your design.
>>
>
>Thanks for the answer. Re-designing is not so easy in this case.
>Basically I extend a 3rd party class with a clever hash-function.
>Unfortunately my sub-class has the effect that the hash-funtion
>degenerates. So I'd like to use the hash-function as it is implemented
>in Object.

    System.identityHashCode() 

    /Mats
Re: calling method of super class of super class
#177
Author: Paul Murray
Date: Fri, 04 May 2001 21:01
21 lines
2140 bytes
"Carsten Friedrich" <Carsten.Friedrich@gmx.de> wrote in message
news:3aee0699.231503@news.usyd.edu.au...
> Hi,
>
> is it possible to call a method of the super class of the super class?
> E.g. (Pseudo java):

No. A class can hide anything it chooses - that's the whole idea of
encapulation. If I write a class X, you can't "back door" the class by
extending it and then directly calling it's superclass methods. If that were
possible, then I would not be able to prove that X always has a valid state.
Java is designed so that this sort of provability is possible. If X does not
do what you want, then you need to speak to the writer of X, or write your
own.

--------------------------------------------------------------
Sometimes he answers "Yes", sometimes "No", sometimes "Wait", and sometimes
he gives you something different.
You get the same set of responses praying to a brick.
http://www.users.bigpond.com/pmurray
ICQ: 26066755
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads