What is runtime polymorphism or dynamic method overloading?

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

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
Updated on: 2020-03-05T12:29:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements