-2

If I have the object and I want to call the version of a method in the parent class is there a way that I can call it like x.super.method() or super.x.method()?

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
NM2
  • 127
  • 6
  • 2
    I see that your question is slightly different than the proposed duplicate. However, I don't think what you are trying to do is even allowed in Java. It doesn't even seem appropriate when you use Object Orient Programming. You should edit your question with some example code if you want to try to get it reopened. – Code-Apprentice Feb 07 '16 at 21:47
  • As it stands I don't think this question should be re-opened. However I agree with @Code-Apprentice that there is the kernel of a valuable question here, which requires editing to bring it out. – APC Feb 07 '16 at 22:18
  • What is the problem with `super.method()`, what more do you expect? – Amir Pashazadeh Feb 07 '16 at 22:38
  • 1
    I want the superclass method of specific variable , not in the method. – NM2 Feb 07 '16 at 22:44
  • I think you have a valid question but it's difficult to understand what you're trying to achieve. Maybe if you define what your super and sub-classes are and explain what method you're trying to access. – mohammedkhan Feb 07 '16 at 22:46
  • 1
    *"I want the superclass method of specific variable , not in the method."* > Then you need to rethink your solution. There are good reasons this is not possible, like breaking contracts etc. Find a solution that does not require this setup. – TT. Feb 07 '16 at 23:13

1 Answers1

0

Not so much an answer as just a little research. This was an interesting question, so just for fun I hacked up some test code. Since comments don't support formatting I'm posting the results here.

I suspect the only way to use super for method invocations and have it actually compile is to use super unqualified, that is with no prefix expression(s).

bar.blarch() compiles and runs as expected (see example, below).

bar.super.blarch() did not compile (which I also expected).

Bar bar = new Bar();
System.out.println("bar.super.blarch() ==> " + bar.super.blarch() );

However, the error message surprised me. It turns out javac complained about "cannot find symbol" as follows:

Bar.java:15: error: cannot find symbol
System.out.println("bar.super.blarch() ==> " + bar.super.blarch() ); // does not compile.
                                               ^
symbol:   class bar
location: class Bar

So at this point it seems like 'super' isn't in the running and javac is instead trying to find an instance variable after the bar. expression.

Then I thought reflection might give you a way to invoke to your preferred method implementation, but that also looks like a No.
Specifically the next to the last example in the output:
"fooBlarch.invoke(bar)=Bar.blarch"
attempts to invoke a method from the Foo class on an instance of type Bar but it is still starting with Bar and invoking the first matching method name + signature.

$ javac *.java
Note: Bar.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$ java Bar
foo.blarch() ==> Foo.blarch
bar.blarch() ==> Bar.blarch
bar.superBlarch() ==> Foo.blarch
moreFoo.blarch() ==> Bar.blarch
fooClass=class Foo
fooBlarch=public java.lang.String Foo.blarch()
fooBlarch.invoke(foo)=Foo.blarch
fooBlarch.invoke(bar)=Bar.blarch
fooBlarch.invoke((Foo)bar)=Bar.blarch
$ 

Foo.java

public class Foo {
   public String blarch() { return "Foo.blarch"; }
}

Bar.java

import java.lang.reflect.*;

public class Bar extends Foo {
   public String blarch() { return "Bar.blarch"; }

   public String superBlarch() { return super.blarch(); }

   public static void main( String[] args ) throws Exception {
      Foo foo = new Foo();
      System.out.println("foo.blarch() ==> " + foo.blarch() ); // prints Foo.blarch

      Bar bar = new Bar();
      System.out.println("bar.blarch() ==> " + bar.blarch() ); // prints Bar.blarch
      System.out.println("bar.superBlarch() ==> " + bar.superBlarch() ); // prints Foo.blarch
      // System.out.println("bar.super.blarch() ==> " + bar.super.blarch() ); // does not compile.
      // Bar.java:15: error: cannot find symbol
      //     System.out.println("bar.super.blarch() ==> " + bar.super.blarch() ); // does not compile.
      //                                                    ^
      //  symbol:   class bar
      //  location: class Bar

      Foo moreFoo = new Bar();
      System.out.println("moreFoo.blarch() ==> " + moreFoo.blarch() ); // prints Bar.blarch

      Class fooClass = foo.getClass();
      System.out.println("fooClass="+fooClass);
      Method fooBlarch = fooClass.getDeclaredMethod( "blarch", new Class[] { } );
      System.out.println("fooBlarch="+fooBlarch);

      System.out.println("fooBlarch.invoke(foo)="+ fooBlarch.invoke( foo ) ); // yields Foo.blarch

      // I think you're out of luck trying to use anything "super-like" outside of a given class.
      // looks like Method.invoke() walks the full instance ancestors looking for a suitable implementation.
      // I actually didn't expect this one to compile given that the method is from class Foo, not Bar.
      System.out.println("fooBlarch.invoke(bar)="+ fooBlarch.invoke( bar ) ); // yields Bar.blarch

      // casting matters not at all.
      System.out.println("fooBlarch.invoke((Foo)bar)="+ fooBlarch.invoke( (Foo)bar ) ); // yields Bar.blarch
   }

}
jgreve
  • 1,225
  • 12
  • 17