Pages

Sunday, October 23, 2016

Java: extends and super

Example: Foo ← Bar

class Foo {
  private String bar;
  Foo(String bar) { this.bar = bar; }
  String foo() { return bar; }
  String bar() { return this.foo(); }
  static String BAR() { return "FOOBAR"; }
}

class Bar extends Foo {
  Bar() { super("BAR"); }
}

class fooBar {
  public static void main(String[] args) {
    System.out.println("Foo.BAR(): " + Foo.BAR());
    Foo foo = new Foo("FOO");
    System.out.println("foo.bar(): " + foo.bar());
    Bar bar = new Bar();
    System.out.println("bar.bar(): " + bar.bar());
  }
}
See Also