Created
September 14, 2011 20:47
-
-
Save aeris/1217746 to your computer and use it in GitHub Desktop.
Java interview question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo1 { | |
String bar = "bar1"; | |
String getBar() { | |
return this.bar; | |
} | |
void setBar(final String bar) { | |
this.bar = bar; | |
} | |
} | |
class Foo2 extends Foo1 { | |
String bar = "bar2"; | |
@Override | |
String getBar() { | |
return this.bar; | |
} | |
} | |
public class Main { | |
public static void main(final String[] args) { | |
final Foo2 foo2 = new Foo2(); | |
final Foo1 foo1 = foo2; | |
foo2.setBar("foo1"); | |
System.out.println(foo2.bar); | |
System.out.println(foo2.getBar()); | |
System.out.println(foo1.bar); | |
System.out.println(foo1.getBar()); | |
foo2.bar = "foo2"; | |
System.out.println(foo2.bar); | |
System.out.println(foo2.getBar()); | |
System.out.println(foo1.bar); | |
System.out.println(foo1.getBar()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment