Calling Overridden Methods without super

Discussion in 'Java' started by Marco, Sep 10, 2004.

  1. Marco

    Marco Guest

    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
     
    Marco, Sep 10, 2004
    #1
    1. Advertising

  2. Andrew Thompson, Sep 10, 2004
    #2
    1. Advertising

  3. Marco

    Chris Uppal Guest

    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
     
    Chris Uppal, Sep 10, 2004
    #3
  4. Marco <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
    - Guardian in "Jinxter"
     
    Joona I Palaste, Sep 11, 2004
    #4
  5. Marco

    Yakov Guest

    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
     
    Yakov, Sep 11, 2004
    #5
  6. Marco

    Frank Guest

    On 10 Sep 2004 08:45:33 -0700, Marco <> 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
     
    Frank, Sep 12, 2004
    #6
  7. Frank <> scribbled the following:
    > On 10 Sep 2004 08:45:33 -0700, Marco <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "He said: 'I'm not Elvis'. Who else but Elvis could have said that?"
    - ALF
     
    Joona I Palaste, Sep 12, 2004
    #7
  8. Marco

    Alex Hunsley Guest

    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
     
    Alex Hunsley, Sep 13, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    13,930
    Chris Uppal
    Sep 24, 2004
  2. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,865
    Darryl Pierce
    Feb 24, 2005
  3. fi3nd
    Replies:
    0
    Views:
    397
    fi3nd
    Apr 8, 2010
  4. Grzegorz Dostatni

    Calling super methods

    Grzegorz Dostatni, Sep 30, 2004, in forum: Ruby
    Replies:
    15
    Views:
    169
  5. Joerg Diekmann

    Calling super in overwritten methods

    Joerg Diekmann, Dec 6, 2006, in forum: Ruby
    Replies:
    9
    Views:
    116
    David Vallner
    Dec 8, 2006
Loading...

Share This Page