Inheritance

M

Mohit Garg

Hi All,

I wish to to if there is any way by which we can access a method of the
Super Base class. (We can not modify base class and super base class
and the method to be accessed has been overridden in both the classes.)

Thanks a lot
Mohit Garg
 
H

hardik

when u call mathod u can use super keyword before accessing super class
mathod.

like super.<mathodname>

may be it will work for u.
 
P

Piotr Kobzda

Mohit said:
I wish to to if there is any way by which we can access a method of the
Super Base class. (We can not modify base class and super base class
and the method to be accessed has been overridden in both the classes.)

The way to achieve this is a transformation of your bytecode to the one
using old 'invokespecial' instruction semantics.

Use the following example as a guide to the solution:

class A {
void m() {}
}

class B extends A {
void m() {}
}

class C extends B { // unset ACC_SUPER flag of the class
void m() {
((A)this).m(); // replace 'invokevirtual' with 'invokespecial'
}
}


The problem with that is I'm not sure all JVMs must support it...


Regards,
piotr
 
C

Chris Uppal

Mohit said:
I wish to to if there is any way by which we can access a method of the
Super Base class. (We can not modify base class and super base class
and the method to be accessed has been overridden in both the classes.)

You can't do it in Java.

As Piotr has mentioned, there is a (very) old semantics for bytecode which
might allow you to hand-generate non-standard bytecodes to do that. Also, it
can certainly be done via JNI (CallNonVirtualXxxMethod()). But the bottom line
is that if you need to do that then your design is fundamentally broken. (Or,
in this case, it may be that the design of the classes you have inherited is
broken.)

-- chris
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top