What is runtime polymorphism or dynamic method overloading?

Java Programming Java8Object Oriented Programming

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Method overriding by a subclass is termed as runtime polymorphism. JVM determines the method to be executed at runtime instead of compile time. 

example

Live Demo

class SuperClass {
   SuperClass get(){
      System.out.println("SuperClass");
      return this;
   }
}
public class Tester extends SuperClass {
   Tester get(){
      System.out.println("SubClass");
      return this;
   }
   public static void main(String[] args) {
      SuperClass tester = new Tester();
 
      tester.get();
   }  
}

Output

SubClass
raja
Published on 23-Apr-2018 00:12:52
Advertisements