Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Calling Overridden Methods without super

Reply
Thread Tools

Calling Overridden Methods without super

 
 
Marco
Guest
Posts: n/a
 
      09-10-2004
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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      09-10-2004
On 10 Sep 2004 08:45:33 -0700, Marco wrote:

> // I want to invoke Alpha.foo() but on the Beta instance.


Why? I suggest do you describe
what you want to achieve.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      09-10-2004
Marco wrote:

> 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



 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      09-11-2004
Marco <(E-Mail Removed)> scribbled the following:
> 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.)

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
 
Reply With Quote
 
Yakov
Guest
Posts: n/a
 
      09-11-2004
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
 
Reply With Quote
 
Frank
Guest
Posts: n/a
 
      09-11-2004
On 10 Sep 2004 08:45:33 -0700, Marco <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      09-12-2004
Frank <(E-Mail Removed)> scribbled the following:
> On 10 Sep 2004 08:45:33 -0700, Marco <(E-Mail Removed)> wrote:
>> 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.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
- ALF
 
Reply With Quote
 
Alex Hunsley
Guest
Posts: n/a
 
      09-13-2004
Marco wrote:

> 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

 
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
Accessing super-class methods from super-class fi3nd Java 0 04-08-2010 03:10 PM
Static methods overridden !! Ravi Java 10 03-26-2007 08:35 AM
Exceptions and overridden methods Milan Gornik C++ 1 03-29-2005 10:55 PM
super.super.super how? Java 24 02-24-2005 10:51 PM
Why can't static methods be overridden? bernd_no_junk@hotmail.com Java 15 09-24-2004 10:42 AM



Advertisments