Java | Runtime Polymorphism

| By Webner

There are two types of polymorphism:

1) Compile Time Polymorphism (Method Overloading) – Here it is clear at compile time that which method call in the program will invoke which of the methods.

2) Runtime Polymorphism (Method Overriding) – Here it is determined at run time that which method needs to be called based on object type.

Runtime Polymorphism Example:

class Parent {
    void print() {
        System.out.println("In Parent Class");

class Child extends Parent {
    void static Parent createObjectInstance(boolean isParent) {
        return isParent ? new Parent() : new Child();

    void print() { //method overriding
        System.out.println("In Child Class");

    public static void main(String args[]) {
        Parent obj = Child.createObjectInstance(Boolean.parseBoolean(args[0]));
        obj.print(); //Runtime Polymorphism

Here createObjectInstance() can return object of Parent or of Child but this cannot be known at compile time, so this is runtime polymorphism. If object of Parent class will be created, print() method of parent will be called else if object of Child will be created, print() method of child will be called at run time.

Leave a Reply

Your email address will not be published. Required fields are marked *