super and overridden method

To access the superclass version of an overridden function, you can do so by using super.

 
class Base {
  int i;

  Base(int a) {
    i = a;
  }

  void show() {
    System.out.println("i: " + i);
  }
}

class SubClass extends Base {
  int k;

  SubClass(int a, int c) {
    super(a);
    k = c;
  }

  void show() {
    super.show(); // this calls A's show()
    System.out.println("k: " + k);

  }
}

public class Main {
  public static void main(String[] argv) {
    SubClass sub = new SubClass(1, 2);
    sub.show();
  }
}

The following output will be generated if you run the code above:


i: 1
k: 2
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.