Java Method Overriding access method from superclass

Introduction

To access the overridden method from superclass, use super keyword.

// Method overriding.
class A {//  ww  w  .ja va  2 s  .c  o  m
  int i, j;

  A(int a, int b) {
    i = a;
    j = b;
  }

  // display i and j
  void show() {
    System.out.println("i and j: " + i + " " + j);
  }
}

class B extends A {
  int k;

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

  void show() {
    super.show(); // this calls A's show()
    System.out.println("k: " + k);
  }
}
  
public class Main {
  public static void main(String args[]) {
    B subOb = new B(1, 2, 3);

    subOb.show(); 
  }
}



PreviousNext

Related