Java Method Overriding Example

Category: Java   Tags: Java, Java Basic, Java Method Overriding

When a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.

When a overridden method is called from within its subclass, it will call the overridden method of subclass and the one in superclass.

OverrideExample1.java

                          package com.tutorial.javabasic;

                          class A {
                              private int num = 0;
                              A(){}

                              A(int n) {
                                  num = n;
                              }

                              void print() {
                                  System.out.println("num in A = " + num);
                              }
                          }

                          class B extends A {
                              private int num = 0;
                              B(int n) {
                                  num = n;
                              }

                              void print() {
                                  System.out.println("num in B = " + num);
                              }
                          }

                          public class OverrideExample1 {

                              public static void main(String s[]) {
                                  A a = new A(200);
                                  a.print();

                                  B b = new B(100);
                                  b.print();
                              }
                          }
                        

Output:

                          num in A = 200
                          num in B = 100
                        

Here B's print method overrides the A's print method and therefore b.print() will call B's print method.

Method overriding occurs only when the names and the type signatures of the two methods are identical. If they are not, then the two methods are simply overloaded.

OverrideExample2.java

                          package com.tutorial.javabasic;

                          class A {

                              void print(int num) {
                                  System.out.println("num in A = " + num);
                              }
                          }

                          class B extends A {
                              private int num = 0;
                              B(int n) {
                                  num = n;
                              }

                              void print() {
                                  System.out.println("num in B = " + num);
                              }
                          }

                          public class OverrideExample2 {

                              public static void main(String s[]) {
                                  A a = new A();
                                  a.print(200);

                                  B b = new B(100);
                                  b.print();
                              }
                          }
                        

Output:

                          num in A = 200
                          num in B = 100
                        

Here method print is overloaded not overridden because both have same name but they differ in signature, one accept argument and other don't.

Why Overridden Methods?

Method overriding allow Java to support run-time polymorphism. It allows the superclass to define a method that will be common to all of its subclasses, while allowing subclass to define all or some of these methods as per its need.

Dynamic Method Dispatch

Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is how Java implements run-time polymorphism. Here is one example:

DynamicRefExample.java

                          package com.tutorial.javabasic;

                          class A {

                              void test() {
                                  System.out.println("test() inside A");
                              }
                          }

                          class B extends A {
                              void test() {
                                  System.out.println("test() inside B");
                              }
                          }

                          class C extends A {
                              void test() {
                                  System.out.println("test() inside C");
                              }
                          }

                          public class DynamicRefExample {

                              public static void main(String s[]) {
                                  B b = new B();
                                  A a = new A();
                                  C c = new C();

                                  a.test();

                                  a = b;
                                  // This will call B's test() method
                                  a.test();

                                  a = c;
                                  // This will call C's test() method
                                  a.test();

                                  /*
                                  This will show compile time error as a child member can not
                                  be accessed using a parent class reference.
                                  */
                                  // a.b();
                              }
                          }
                        

Output:

                          test() inside A
                          test() inside B
                          test() inside C
                        

Here since A is parent class of class B and C, the statement a = b; and a = c; are a valid statement.

a.b() will throw error as you can't call a subclass method from a parent class object reference and this is because that a parent class has no knowledge about its subclass.