Method overriding vs method overload

Method overriding occurs when the names and the type signatures of the two methods are identical.

If not, the two methods are overloaded.

For example, consider this modified version of the preceding example:

 
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(String msg) {
    System.out.println(msg + k);
  }
}
public class Main {
  public static void main(String args[]) {
    SubClass subOb = new SubClass(1, 2);
    subOb.show("This is k: "); 
    subOb.show(); 
  }
}

The output produced by this program is shown here:


This is k: 2
i: 1
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