Difference between runtime and compile time polymorphism?

Polymorphism is defined as one interface to control access to a general class of actions. There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is functions and operators overloading. Runtime time polymorphism is done using inheritance and virtual functions.

Polymorphism means that functions assume different forms at different times. In case of compile time it is called function overloading. For example, a program can consist of two functions where one can perform integer addition and other can perform addition of floating point numbers but the name of the functions can be same such as add. The function add() is said to be overloaded. Two or more functions can have same name but their parameter list should be different either in terms of parameters or their data types. The functions which differ only in their return types cannot be overloaded. The compiler will select the right function depending on the type of parameters passed. In cases of classes constructors could be overloaded as there can be both initialized and uninitialized objects. Here is a program which illustrates the working of compile time function overloading and constructor overloading.
eg.
namespace CommonClasses
{
    public interface IAnimal
    {
        string Name { get; }
        string Talk();
    }
}
 
<i>// Assembly: Animals</i>
using System;
using CommonClasses;
 
namespace Animals
{
    public abstract class AnimalBase
    {
        public string Name { get; private set; }
 
        protected AnimalBase(string name) {
            Name = name;
        }
    }
 
    public class Cat : AnimalBase, IAnimal
    {
        public Cat(string name) : base(name) {
        }
 
        public string Talk() {
            return "Meowww!";
        }
    }
 
    public class Dog : AnimalBase, IAnimal
    {
        public Dog(string name) : base(name) {            
        }
 
        public string Talk() {
            return "Arf! Arf!";
        }
    }
}
 
<i>// Assembly: Program</i>
<i>// References and Uses Assemblies: Common Classes, Animals</i>
using System;
using System.Collections.Generic;
using Animals;
using CommonClasses;
 
namespace Program
{
    public class TestAnimals
    {
        <i>// prints the following:</i>
        <i>//   Missy: Meowww!</i>
        <i>//   Mr. Bojangles: Meowww!</i>
        <i>//   Lassie: Arf! Arf!</i>
        <i>//</i>
        public static void Main(string[] args)
        {
            var animals = new List<IAnimal>() {
                new Cat("Missy"),
                new Cat("Mr. Bojangles"),
                new Dog("Lassie")
            };
 
            foreach (var animal in animals) {
                Console.WriteLine(animal.Name + ": " + animal.Talk());
            }
        }
    }
}

..............................................................................................................................................................

The term static polymorphism is associated with overloaded methods because it gives the impression that a single named method will accept a number of different argument types. The System.out.println() method is an example that may take String or Object references boolean and other primitive types as an argument. In fact each overloaded method is separate and the compiler can see the difference between them. In this case the argument types are fixed at compile time and are considered static. This has nothing to do with the Java keyword static. Dynamic polymorphism is where a class overrides a superclass method or implements an interface. For example any class may override the Object.toString() method and provide its own implementation and this is known at compile time. However for a simple Java program that instantiates a series of objects and calls their toString() method the compiler does not consider the object references differently. Any differences in the objects' toString() implementations are only seen at runtime so they are considered dynamic.


...............................................................................................................................................................

The term static polymorphism is associated with overloaded methods
because it gives the impression that a single named method will accept
a number of different argument types. The System.out.println() method
is an example that may take String or Object references, boolean and
other primitive types as an argument. In fact, each overloaded method
is separate and the compiler can see the difference between them. In
this case, the argument types are fixed at compile time and are
considered static. This has nothing to do with the Java keyword
static.
Dynamic polymorphism is where a class overrides a superclass method or
implements an interface. For example, any class may override the
Object.toString() method and provide its own implementation, and this
is known at compile time. However, for a simple Java program that
instantiates a series of objects and calls their toString() method,
the compiler does not consider the object references differently. Any
differences in the objects' toString() implementations are only seen
at runtime, so they are considered dynamic.



6 comments:

shajil said...

hi friend your post good,but i cant understand what is the difference between the polymorphism. can you plz let me know with small example of this concept.thanks a lot ....

Digi said...

Thank you for this blog
Learn Digital marketing course for free online
[url=https://clicksbazaar20.wordpress.com/2021/09/23/top-6-essential-google-chrome-extensions-for-seo/amp/]Digital Marketing company In Chennai[/url]
[url=https://clicksbazaar2.blogspot.com/2021/09/why-local-event-marketing-matters-to.html]Digital Marketing Agency In Mumbai[/url]
[url=https://ext-5832936.livejournal.com/2122.html]Digital Marketing Agency In Noida[/url]
[url=https://sites.google.com/view/clicks-bazaar-best-digital-mar/blogs/impact-of-big-data-on-the-future-of-digital-marketing]advertising agencies in hyderabad[/url]
[url=best digital marketing agency in bangalore]best digital marketing agency in bangalore[/url]

David said...

Thank you for this valuable blog .
Learn digital marketing online for free and earn upto 3k everyday
best digital marketing company in chennai
Best Digital Marketing Agency In Mumbai
Digital Marketing Agency Mumbai
Digital Marketing Company In Mumbai
Digital Marketing Company In Mumbai

Rohit said...

Thanks for the post and knowledge
Valentine’s Day Gifts
Best valentine's day gifts for girlfriend
Valentine’s day gifts for her
Chocolate Day Gift
Valentine Chocolate Day Gift

Digi said...

Thank you fo this helpful post. I was waiting for this
If you are looking for best SEO Company in India for your business in a affordable price here are the best SEO Companies in India , which will help you to boost your business activities with proper optimization techniques.

1.Top SEO Compamny in Delhi NCR
2..Best SEO Company in Delhi NCR
3.SEO Compnay in Delhi NCR
4.Best SEO Compnay in India
5.Top SEO Company in India
6.Affordable SEO Company in India

Unknown said...

Thank you for this informational Blog. if you are interested to learn more informative and Helpful blog How Core Web Vitals Affect Google’s Algorithms

Your SEO ranking may increase or decrease based on this new Core Web Vitals Affect Google algorithm update.

Google has announced that Core Web Vitals will become ranking signals starting May 2021. The update will also integrate existing UX-related signals. While we spend time focusing on the mobile experience, keyword optimization, and backlinks, Google has turned its attention to the on-page experience.

In brief, Google likes to conclude, they will start using Page Experience as part of their ranking systems.

So, in this post, we’ll find out what exactly that means so that your website doesn’t get penalized and lose rankings. Instead, you can use this update to increase the rankings beyond your competitors.

Today we’re going to discuss this new algorithm update from Google and help you prepare your business for it. Alright, let’s take a look at How Core Web Vitals Affect Google’s Algorithms

While we spend a lot of time thinking about keyword optimization, mobile-friendliness, and backlinks, Google is very concerned with the on-page experience. That’s why they’ve launched Core Web Vitals, a new collection of signals. This is part of the new “page experience” signals that were implemented in May 2021.

The page loading speed, responsiveness, and imaged stability of a website will all be considered in these signals.

Post a Comment