Dynamic Method Dispatch in Run-Time Polymorphism

What is Polymorphism?

As part of Object-Oriented Programming, polymorphism is considered one of the most important features. The concept of polymorphism enables us to perform the same action differently. In other words, polymorphism allows you to specify one interface but implement multiple methods. As "poly" means many, and "morphs" means forms, it means that there are many forms

Polymorphism Types

Polymorphism in Java mainly consists of two types:

  • Compile-time Polymorphism
  • Runtime Polymorphism

Compile-time Polymorphism

Alternatively, it is referred to as static polymorphism. The function overloading or operator overloading technique get used to achieve this type of polymorphism.

Runtime Polymorphism

Runtime polymorphism in Java is also called Dynamic Method Dispatch or Dynamic Binding. This process involves dynamically resolving a call to an overridden method at runtime rather than compiling it. Method overriding allows you to achieve runtime polymorphism.

Syntax

You should use annotations in Java to implement runtime polymorphism.

@Override

The developer may use this annotation to indicate which method needs to get overridden. The method override occurs when the parent class or the superclass defines the same method name, parameters, and return type as the child class or subclass; thus, the child class overrides the superclass's method.

In simpler terms, a subclass may implement a method that already exists in the superclass; in this case, the function in the parent class is said to have been overridden. Also, data members cannot support runtime polymorphism; only functions can. Superclass reference variables are used to override.

Reference variables are used to determine which method must be called for the object they refer to. The process is also referred to as upcasting. An upcast occurs when a reference variable in the parent class refers to an object in the child class. For example:

class X{}
class Y extends X{} 
X x=new Y(); //upcasting

Rules of Runtime Polymorphism

The Runtime polymorphism has the following rules:

  • Parent and child classes should have the same method names.
  • The child's and the parent's methods should have the same parameters.
  • IS-A relationships must be established.
  • A parent's private methods cannot be overridden.
  • Static methods cannot be overridden.
  • Final methods cannot be overridden.

Code Example

class Person {
    void eat() {
        System.out.println("Person is eating");
    }
}
class Man extends Person {
    void eat() {
        System.out.println("Man is eating fruits");
    }
}
class child extends Man {
    void eat() {
        System.out.println("Child is eating Apple");
    }
}
public class RunTimePolymorphism {
    public static void main(String args[]) {
        Person superObject=new Person();
        Person subObject=new Man();
        Person childObject=new child();
        superObject.eat();
        subObject.eat();
        childObject.eat();
    }
}

Output
Person is eating
Man is eating fruits
Child is eating Apple

write your code here: Coding Playground