Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Quick inheritance question

Reply
Thread Tools

Quick inheritance question

 
 
Will Clark
Guest
Posts: n/a
 
      08-23-2003
I have a class such as:

class A
{
public void methodA()
{
// does general stuff and also:

methodB();
}

public void methodB()
{
// Does something non-descript!
}
}

and another class that is a subclass of this, for example:

class B extends A
{
public void methodA()
{
// Does stuff and then...

super.methodA();
}

public void methodB()
{
// Does something or other and then...

methodA();
}
}

Now if I were to create an instance of class B and call methodB on it, such
as in the following fragment:

B obj;
obj.methodB();

I would like it to run, in the following order:

B.methodB()
B.methodA()
A.methodA()
A.methodB()

end then stop! But it doesn't because instead A.methodA() goes and calls
B.methodB() not A.methodB() and the whole thing sits in a loop.

Is there any way of indicating to Java that A.methodA() calls A.methodB()
and NOT the inherited version of it?

Cheers for your help!

Will






 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      08-23-2003
Will Clark <(E-Mail Removed)> scribbled the following:
> I have a class such as:


> class A
> {
> public void methodA()
> {
> // does general stuff and also:


> methodB();
> }


> public void methodB()
> {
> // Does something non-descript!
> }
> }


> and another class that is a subclass of this, for example:


> class B extends A
> {
> public void methodA()
> {
> // Does stuff and then...


> super.methodA();
> }


> public void methodB()
> {
> // Does something or other and then...


> methodA();
> }
> }


> Now if I were to create an instance of class B and call methodB on it, such
> as in the following fragment:


> B obj;
> obj.methodB();


> I would like it to run, in the following order:


> B.methodB()
> B.methodA()
> A.methodA()
> A.methodB()


> end then stop! But it doesn't because instead A.methodA() goes and calls
> B.methodB() not A.methodB() and the whole thing sits in a loop.


> Is there any way of indicating to Java that A.methodA() calls A.methodB()
> and NOT the inherited version of it?


No. Instance methods always use the most specific implementation, i.e.
furthest down in the inheritance tree. You'll have to use a real
instance of A (not just a B pretending to be an A) or make the methods
static.

> Cheers for your help!


> Will








--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
- Dr. Michael Hfuhruhurr
 
Reply With Quote
 
 
 
 
Robert Olofsson
Guest
Posts: n/a
 
      08-24-2003
Joona I Palaste ((E-Mail Removed)) wrote:
: No. Instance methods always use the most specific implementation, i.e.
: furthest down in the inheritance tree. You'll have to use a real
: instance of A (not just a B pretending to be an A) or make the methods
: static.

I can think of one thing that may work (I have not tested this so I am
not certain that it works). With reflection it is possible to find the
super class method and it should be possible to call that
method. Using reflections for this case is _not_ recomended, but it
may work.
Anyone care to test?

/robo
 
Reply With Quote
 
Gerbrand van Dieijen
Guest
Posts: n/a
 
      08-25-2003
On Sun, 24 Aug 2003 00:24:08 GMT, David E. wrote:
>couldn't he (in the "3rd step") use casting somehow to call methodB of class
>A?


You can use super.method() to call a method of the class it inherited.


--
Gerbrand van Dieijen

WWW: http://twisted.warande.net
ICQ: 19345450



 
Reply With Quote
 
Fred L. Kleinschmidt
Guest
Posts: n/a
 
      08-26-2003


Will Clark wrote:
>
> I have a class such as:
>
> class A
> {
> public void methodA()
> {
> // does general stuff and also:
>
> methodB();
> }
>
> public void methodB()
> {
> // Does something non-descript!
> }
> }
>
> and another class that is a subclass of this, for example:
>
> class B extends A
> {
> public void methodA()
> {
> // Does stuff and then...
>
> super.methodA();
> }
>
> public void methodB()
> {
> // Does something or other and then...
>
> methodA();
> }
> }
>
> Now if I were to create an instance of class B and call methodB on it, such
> as in the following fragment:
>
> B obj;
> obj.methodB();
>
> I would like it to run, in the following order:
>
> B.methodB()
> B.methodA()
> A.methodA()
> A.methodB()
>
> end then stop! But it doesn't because instead A.methodA() goes and calls
> B.methodB() not A.methodB() and the whole thing sits in a loop.
>
> Is there any way of indicating to Java that A.methodA() calls A.methodB()
> and NOT the inherited version of it?
>
> Cheers for your help!
>
> Will


the easy way is to create a third method in class A:
class A
{
public void methodA()
{
// does general stuff and also:

methodC();
}

public void methodB()
{
methodC();
}

private void methodC()
{
// Does the real stuff for classA's methodB
}
}

--
Fred L. Kleinschmidt
Associate Technical Fellow
Boeing Common User Interface Services
 
Reply With Quote
 
Will Clark
Guest
Posts: n/a
 
      08-27-2003
I agree that your solution is the only way you can do it using the Java
compiler. I have come to the conclusion that it appears to be a "limitation"
of the Java compiler, because my tests have indicated that it is possible if
you modify the call at the bytecode level.

Java does a "invokevirtual" to call a method, and it calls whatever
overrides the virtual method in class A with the method in class B, causing
the infinite loop.

However, if the "invokevirtual" instruction is replaced with "invokespecial"
or "invokenonvirtual" then it works as needed.

(I have attached three source files - A.java and a rewritten A.jasm (for the
Jasmin bytecode assembler, and B.java - and the compiled class files. Try
running "java B" and the behaviour is correct.)

Does anyone know of any short comings to using this approach? According to
the official VM specs "invokespecial" is mainly for use in calling <init> or
when compiling the 'super' keyword. And "invokenonvirtual" doesn't seem to
be documented anywhere...

Will

P.S. perhaps it would be nice if an up-and-coming Java compiler could add a
keyword that invokes a method as though it were non overriden?! (A
'me.methodB()' construction perhaps!)













 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick question, hopefully quick answer. ~misfit~ NZ Computing 114 01-06-2005 01:36 PM
Quick Question Quick Answer JKop C++ 11 05-24-2004 09:46 PM
Quick Restore for a Compaq not so quick! Croos Bustamunky Computer Support 2 05-15-2004 04:17 AM
PanasonicBQ390 "quick" charger - How quick? Ol' Bab Digital Photography 1 01-17-2004 06:54 AM



Advertisments