Calling Overridden Methods without super

M

Marco

Can I call the overridden version of an object's foo() method
outside that object's class, without using super? EG:

class MainProgram {
public static void main(String[] args) {
Beta b = new Beta();

// I want to invoke Alpha.foo() but on the Beta instance.
// I want to cancel the dynamic binding that makes Beta.foo()
// get called instead
// b.super.foo() <---- obviously doesn't compile
}
}

class Alpha {
public void foo() { System.out.println("inside Alpha.foo()"); }
}

class Beta extends Alpha {
public void foo() { System.out.println("inside Beta.foo()"); }
}

Is this possible?
Marco
 
C

Chris Uppal

Marco said:
Can I call the overridden version of an object's foo() method
outside that object's class, without using super?

Not directly. That's to say, there's no way that the Java language can express
that.

You may be able to do it via reflection -- find the instance of Method in the
superclass, and then invoke() that against your subclass object. That /ought/
to work, I think, although it wasn't very clearly defined last time I looked,
and it's not something I've tested myself. (And is such an odd case that I'd
be wary of assuming that it worked on all Java implementations, even if it does
work on some/most.)

Alternatively, you can definitely do it via JNI (I know because I do it).

The chances are, though, that there's a better way to do whatever it is you are
really trying to achieve.

-- chris
 
J

Joona I Palaste

Marco said:
Can I call the overridden version of an object's foo() method
outside that object's class, without using super? EG:
class MainProgram {
public static void main(String[] args) {
Beta b = new Beta();
// I want to invoke Alpha.foo() but on the Beta instance.
// I want to cancel the dynamic binding that makes Beta.foo()
// get called instead
// b.super.foo() <---- obviously doesn't compile
}
}
class Alpha {
public void foo() { System.out.println("inside Alpha.foo()"); }
}
class Beta extends Alpha {
public void foo() { System.out.println("inside Beta.foo()"); }
}
Is this possible?
Marco

No, and it shouldn't be. It is Beta's decision whether it wants to use
Alpha's foo() or its own, not the calling code's. Allowing code outside
the Beta class to influence the binding of its methods breaks the
concepts of polymorphism and encapsulation.
(If you absolutely insist, you might try using Reflection. Personally,
I find any use of Reflection a crude hack. I have never needed to use it
anywhere.)
 
Y

Yakov

As a workaround, add to the subclass a method

public void callParent(boolead shouldIcallSuper){
this.callSuper = shouldIcallSuper;
}


And here's the foo() in the subclass:

foo(){
if (callSuper){
super.foo();
}else{
// your current code
}
}

Regards,
Yakov
 
F

Frank

Can I call the overridden version of an object's foo() method
outside that object's class, without using super? EG:

class MainProgram {
public static void main(String[] args) {
Beta b = new Beta();

// I want to invoke Alpha.foo() but on the Beta instance.
// I want to cancel the dynamic binding that makes Beta.foo()
// get called instead
// b.super.foo() <---- obviously doesn't compile
}
}

class Alpha {
public void foo() { System.out.println("inside Alpha.foo()"); }
}

class Beta extends Alpha {
public void foo() { System.out.println("inside Beta.foo()"); }
}

Is this possible?
Marco

There are some other responses here, but I've noticed most people have
said no, but the answer is actually yes (sorta):

change
class Beta extends Alpha {
public void foo() { System.out.println("inside Beta.foo()"); }
public void origionalFoo() { super.foo(); }
}

then your main method reads:

Beta b=new Beta();
b.origionalFoo();

HTH

-Frank
 
J

Joona I Palaste

Frank said:
Can I call the overridden version of an object's foo() method
outside that object's class, without using super? EG:

class MainProgram {
public static void main(String[] args) {
Beta b = new Beta();

// I want to invoke Alpha.foo() but on the Beta instance.
// I want to cancel the dynamic binding that makes Beta.foo()
// get called instead
// b.super.foo() <---- obviously doesn't compile
}
}

class Alpha {
public void foo() { System.out.println("inside Alpha.foo()"); }
}

class Beta extends Alpha {
public void foo() { System.out.println("inside Beta.foo()"); }
}

Is this possible?
Marco
There are some other responses here, but I've noticed most people have
said no, but the answer is actually yes (sorta):
change
class Beta extends Alpha {
public void foo() { System.out.println("inside Beta.foo()"); }
public void origionalFoo() { super.foo(); }
}
then your main method reads:
Beta b=new Beta();
b.origionalFoo();

The point is that this, like the other reply with a workaround,
requires a change to Beta's code. If Beta includes only one definition
of foo(), and that does not explicitly include a call to super.foo(),
then it is impossible to make foo() in Beta call foo() in Alpha from
other code, without modifying the code in Beta.
From the OP's original question I got the idea that he was trying to
circumvent the overriding without modifying the code in Beta. If this
were possible, it would even present a security risk in some
applications, when the behaviour of code in a class could be changed
without the class being able to do anything about it.
 
A

Alex Hunsley

Marco said:
Can I call the overridden version of an object's foo() method
outside that object's class, without using super? EG:

This is really not a good idea. No idea what your situation or constraint is,
but the fact you're trying to do this suggests to me that you should rethink
the design or find a different way around things.

As someone else suggested, it should be up to the target class whether it uses
its own code, calls super, or whatever. Inheritence of class A should be
transparent as far as anotehr class, B, that uses A is concerned.

alex
 

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,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top