java - Correct way of writing "super" -
this question has answer here:
if have object , want call version of method in parent class there way can call x.super.method() or super.x.method()?
not answer little research. interesting question, fun hacked test code. since comments don't support formatting i'm posting results here.
i suspect way use super method invocations , have compile use super unqualified, no prefix expression(s).
bar.blarch() compiles , runs expected (see example, below).
bar.super.blarch() did not compile (which expected).
bar bar = new bar(); system.out.println("bar.super.blarch() ==> " + bar.super.blarch() ); however, error message surprised me. turns out javac complained "cannot find symbol" follows:
bar.java:15: error: cannot find symbol system.out.println("bar.super.blarch() ==> " + bar.super.blarch() ); // not compile. ^ symbol: class bar location: class bar so @ point seems 'super' isn't in running , javac instead trying find instance variable after bar. expression.
then thought reflection might give way invoke preferred method implementation, looks no.
specifically next last example in output:
"fooblarch.invoke(bar)=bar.blarch"
attempts invoke method foo class on instance of type bar still starting bar , invoking first matching method name + signature.
$ javac *.java note: bar.java uses unchecked or unsafe operations. note: recompile -xlint:unchecked 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() ); // not compile. // bar.java:15: error: cannot find symbol // system.out.println("bar.super.blarch() ==> " + bar.super.blarch() ); // 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 // think you're out of luck trying use "super-like" outside of given class. // looks method.invoke() walks full instance ancestors looking suitable implementation. // didn't expect 1 compile given method class foo, not bar. system.out.println("fooblarch.invoke(bar)="+ fooblarch.invoke( bar ) ); // yields bar.blarch // casting matters not @ all. system.out.println("fooblarch.invoke((foo)bar)="+ fooblarch.invoke( (foo)bar ) ); // yields bar.blarch } }
Comments
Post a Comment