Java - Method Overriding and super Accessing

How to access overridden method?

To access the overridden method from a subclass, use the keyword super as a qualifier.

Syntax

super.methodName

Demo

class MySuper {
  public void print() {
    System.out.println("Inside MySuper.print()");
  }/*from  www . j ava 2 s  .  c  o  m*/
}

class MySub extends MySuper {
  public void print() {
    // Call print() method of MySuper class
    super.print();

    // Print a message
    System.out.println("Inside MySub.print()");
  }

  public void callOverridenPrint() {
    // Call print() method of MySuper class
    super.print();
  }
}

public class Main {
  public static void main(String[] args) {
    MySub MySub = new MySub();
    MySub.print();
    MySub.callOverridenPrint();
  }
}

Result

Related Topic