class - Java riddle: static binding -
i'm given class c
, interface a
, i'm not allowed changes in them. should write class b
code output success!
there 2 things don't understand in code of class c
inside loop.
1) don't understand bar method called.
2) when return reached return to?
should change implementation of b
in order work?
public interface { public string bar(); } public class c extends b { public int foo = 100; public c(string s) { super(s); } @override public void barbar() { foo++; } public static void main(string[] args) { string input = args[0]; stringbuilder builder = new stringbuilder(input); c c = new c(input); //c=args[0] b b = c; //b=args[0] a = b; //a=args[0] (int = 0; < args.length; i++) { if (!builder.tostring().equals(a.bar())) { //which bar called? return; //where return to? exist loop? } builder.append(input); c.foo++; } if (c.foo - 99 == b.foo.length() / input.length()) { system.out.println("success!"); } } } public class b implements { public string foo; public b(string s) { this.foo=s; } public void barbar() { // todo auto-generated method stub } @override public string bar() { return this.foo; } }
bar()
defined inclass b
called ofcourse. if believe methodinterface a
called, must tell methods interface never called. interfaces have method declarations(unless it'sdefault
method) implementing non-abstract class has define. , linereturn this.foo
executed.return
statementmain
method ends program. returns jvm, in turn returns os.
Comments
Post a Comment