Static Polymorphism Dynamic Polymorphism

Static Polymorphism Dynamic Polymorphism

Static Polymorphism Dynamic Polymorphism

Summary: By the end of this tutorial "Static Polymorphism Dynamic Polymorphism", you will understand their differences. Explained with programs in simple words.

It is the continuation of previous program Difference: Static binding and Dynamic binding and knowledge of binding is very much required for better understanding this concept as binding leads to polymorphism or without binding there is no polymorphism.

A) Program on Static Polymorphism

In the above example, calculate() method is called four times and each time it gives different outputs. This is called Polymorphism. In an OOPs language, the same method called different times gives different outputs is known as Polymorphism (poly=many, morphism=form). Otherway, many forms of the same method. For example, I say God is polymorphic. God is only one but has different forms like Lord Balaji, Goddess Laxmi, Goddess Saraswathi and infact, each one fulfils different wishes like Chilkur Balaji in Hyderabad helps in Visa stamping.

As this polymorphism is achieved through static binding, it is known as static polymorphism.

Method overloading leads to static binding and static binding leads to static polymorphism.

Method overloading –> Static Binding –> Static Polymorphism

B) Dynamic Polymorphism

Observe the program given in the same "Static binding and Dynamic bindingg".

Here also, the same method call() called different times gives different outputs of calling Raju, Prasad and Jyostna with different advices. This is known as polymorphism. As polymorphism is achieved through dynamic binding, it is known as dynamic polymorphism.

Principle involved in Dynamic binding

l1 = r1;;

If a subclass object is assgined to super class object, the super class object will call subclass overridden method. Observe, when r1 is assigned to l1, super class l1 will call subclass r1 call() method.

Property Static Polymorphism Dynamic Polymorphism
Occurrence Occurs at compile-time Occurs at runtime
Achievement Achieved through static binding Achieved through dynamic binding
Required methods Method overloading should exist Method overriding should exist
Inheritance Inheritance is not involved Inheritance is involved
Classes Happens in the same class Happens between two classes
Object assignment Object assignment is not required Required where a subclass object is assigned to super class object

Pass your suggestions for the betterment of this tutorial "Static Polymorphism Dynamic Polymorphism".

View All for Java Differences on 90 Topics

4 thoughts on “Static Polymorphism Dynamic Polymorphism

  1. Muhammad Jamil

    It’s so informative website
    my question is ,when superclass object refer to subclass object and super class object belong to superclass and can access all super class methods as well as fields i mean it’s purely superclass object .why superclass object can not call subclass methods that are not overriden as well as fields . i m thanks

Leave a Reply

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