Q-16: Write a JAVA program to implement the concept of Method Overriding (Run Time Polymorphism).
  

Method Overriding


// Contributed by - Anuj Das ( GC College, Silchar - @ Department of Computer Science ) class Parent { void show() { System.out.println("Parent's show() invoked!"); } } class Child extends Parent { // This method overrides show() of Parent @Override void show() { System.out.println("Child's show() invoked!"); } } class MethodsOverriding { public static void main(String[ ] args) { // If a Parent type reference refers to a Parent object, then Parent's show is called. Parent obj1 = new Parent(); obj1.show(); // If a Parent type reference refers to a Child object Child's show() is called. // This is called RUN TIME POLYMORPHISM. Parent obj2 = new Child(); obj2.show(); } }

OUTPUT

cmd->  javac  MethodsOverriding.java
cmd->  java  MethodsOverriding

Parent's show() invoked!
Child's show() invoked!