On 29 Mar 2001 22:02:14 -0500, Dimitris Vyzovitis wrote:
> This is totally off topic, but somebody may have an idea... You can 
> answer (or flame me ;) in private to avoid contaminating the list further.
> 
> Is there any way to invoke a method for a class two levels up the hierarchy?

Not really.  I would ask a more fundamental question: why do you need to
do this?  What kind of design absolutely requires this?  Considering
that the whole JDK, any number of Java applications including TogetherJ,
Forte for Java, JBuilder, many EJB application servers, many XML parsers
and transformers were designed and implemented without requiring this
feature, I would strongly believe that there must be a way to solve your
problem without resorting to the above mentioned feature.

Maybe a design like this will solve your problem:

interface B {
    void doit();
}

class A implements B {
    public void doit() { /* default implementation */ }
}

class C implements B {
    public void doit() { /* C does not use default implementation */ }
}

class D extends A {
    public void doit() { /* D uses the default implementation */
super.doit(); /* more */ }
}

> To be more specific, consider the following scenario:
> class A {
>       void doit() {
>               // ...
>       }
> }
>       
> abstract class B extends A {
>       abstract void doit();
> }
>       
> class C extends B {
>       void doit() {
>               // ...
>       }
> }
> 
> Now, say that an instance of C wants to invoke the method implementation 
> in A. Apparently, there is no way to do it... 
> 
> Neither a reflective invokation or a direct invokation will work. 
> That is, in an instance of C,
> super.doit() is invalid (since doit is an abstract method in B), while
> A.class.getDeclaredMethod( "doit", null ).invoke( this, null ) will 
> invoke the method implementation in C, which overrides the method 
> implementation in A, hence falling into an infinite loop.

That's what is meant when they say C.doit() overrides the method of the
same name in its super classes.

> 
> Is there any way to work around this, apart from declaring B like this: 
> abstract class B {
>       void doit() { super.doit(); }
> }
> (this is not satisfactory, because you may want to force B subclasses to 
> override doit(), and a declaration like this may allow C to compile without
> doing so).
-- 
Weiqi Gao
[EMAIL PROTECTED]


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to