Skip to content

Instantly share code, notes, and snippets.

@aeris
Created September 14, 2011 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeris/1217746 to your computer and use it in GitHub Desktop.
Save aeris/1217746 to your computer and use it in GitHub Desktop.
Java interview question
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