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
Home 
  Java Book 
    Class  

Inheritance:
  1. Inheritance Basics
  2. When Constructors Are Called
  3. Using super keyword
  4. What is Method Overriding
  5. super and overridden method
  6. Method overriding vs method overload
  7. Dynamic Method Dispatch
  8. final variables