What is Polymorphism in Java with Real-Time Example

1. Objective

In our last session, we discussed Exception Handling in Java. Here, we are going to study what is Java Polymorphism. Moreover, we are going to learn about the Dynamic Method Dispatch or Runtime Polymorphism in Java and thus understand the concept of polymorphism in object-oriented programming.

So, let’s start Polymorphism in Java.

Polymorphism in Java

What is Polymorphism in Java with Real-Time Example

2. What is Polymorphism in Java?

Dynamic Method Dispatch or Runtime Polymorphism is supported by method overriding. Dynamic method dispatch helps to resolve a method override at the runtime rather at compile time.

  • Java language determines which version of Java method is to be called and executed based on the type of object being referred when an overridden method is called via a superclass reference.
  • During the runtime, the type of Java object, not the reference variable determines which version of the overridden method is to be executed.
  • A superclass reference as a reference to a subclass object it is known as upcasting in java. This method use by Java to resolve calls to overridden methods at runtime.

Do you know about What is Encapsulation in Java?

What is Polymorphism in Java with Real-Time Example

Java Polymorphism

3. Example to Illustrate Dynamic Method Dispatch

Polymorphism real-time example gives a better understanding of polymorphism in Java.

class A
{
    void m1()
       {
            System.out.println("Inside A's m1 method");
       }
}
class B extends A
   {
// overriding m1()
      void m1()
           {
             System.out.println("Inside B's m1 method");
           }
    }
class C extends A
    {
// overriding m1()
       void m1()
          {
             System.out.println("Inside C's m1 method");
         }
   }
// Driver class
class Dispatch
   {
        public static void main(String args[])
             {
// object of type A
                  A a = new A();
                  B b = new B();
                  C c = new C();
// obtain a reference of type A
                  A ref;
// ref refers to an A object
                  ref = a;
// calling A's version of m1()
                  ref.m1();
                  ref = b;
                  ref.m1();
                  ref = c;
                  ref.m1();
            }
   }

Let’s Revise Java Access Modifiers.

Explanation of the program above 

In the above example of Polymorphism in Java, a superclass A create with its two subclasses B and C.

  • In main() classes A, B and C create.

Example to Illustrate Dynamic Method Dispatch

Example to Illustrate Dynamic Method Dispatch

  • A reference type of A called ref which will be null initially.

Follow this link to know about Interface in Java with Example

  • Now a reference assigns to each type of object to the ref, and thus uses m1() invoke a method.
Dynamic Method Dispatch

Dynamic Method Dispatch

Read about Serialization and Deserialization in Java

4. Runtime Polymorphism with Data Members

class A
     {
           int x = 10;
     }
class B extends A
      {
        int x = 20;
      }
// Driver class
public class Test
       {
           public static void main(String args[])
              {
                    A a = new B(); // object of type B
                    System.out.println(a.x);
              }
      }

Advantages of Dynamic Method Dispatch – It allows Java language to support overriding of methods which supports run-time polymorphism in java. It also allows the classes to specify the methods which are common to the derivatives. It also allows subclasses to add their own specific methods subclasses.

So, this was all about Polymorphism in Java. Hope you like our explanation.

5. Conclusion

Hence, we learned about polymorphism in Java, how runtime polymorphism implements, requirements of a dynamic method dispatch with the example of Java Polymorphism. Furthermore, if you have any doubt, feel free to ask in the comment box.

See also – Best Java books

For reference

Leave a Reply

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