<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Viraj Guranna Yalawar</title>
    <description>The latest articles on DEV Community by Viraj Guranna Yalawar (@viraj_guranna).</description>
    <link>https://dev.to/viraj_guranna</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4123666%2F089b1df5-b31d-4204-b389-51471fb3d7fb.png</url>
      <title>DEV Community: Viraj Guranna Yalawar</title>
      <link>https://dev.to/viraj_guranna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/viraj_guranna"/>
    <language>en</language>
    <item>
      <title>Due to the if-else and switch statements ,run time polymorphism exist in this world !!!</title>
      <dc:creator>Viraj Guranna Yalawar</dc:creator>
      <pubDate>Thu, 01 Oct 2026 12:05:20 +0000</pubDate>
      <link>https://dev.to/viraj_guranna/due-to-the-if-else-and-switch-statements-run-time-polymorphism-exist-in-this-world--3ph4</link>
      <guid>https://dev.to/viraj_guranna/due-to-the-if-else-and-switch-statements-run-time-polymorphism-exist-in-this-world--3ph4</guid>
      <description>&lt;p&gt;Yes, you heard it right. The reason that run-time polymorphism exists in programming is due to the decision-making statements that exist in programming. To understand that, quickly, we will understand the following terminologies:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compile time:&lt;/strong&gt; is the time when the compiler is scanning each line of code and converting it to the machine-level instructions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run time:&lt;/strong&gt; is the time when the machine is executing the instructions in a proper order as decided by the compiler.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method binding:&lt;/strong&gt; is the process of binding the function call with the implementation of a method.&lt;/p&gt;

&lt;p&gt;In method overloading, beforehand only, we write the implementations of all the required methods with different number of parameters or different data types .&lt;/p&gt;

&lt;p&gt;In method overriding also, we write the implementation beforehand only; for example, in a derived class, we write the complete implementation of the method of the base class, which needs to be overridden by the derived class.&lt;/p&gt;

&lt;p&gt;So it is mandatory that both types of methods get compiled before the execution of the main function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method Overloading
&lt;/h2&gt;

&lt;p&gt;If we consider the case of method overloading, we know that at compile time only, the machine gets to know whether there are two parameters, three parameters, or whether it needs to differentiate the data types and bind the correct method with the line having the method call in the main method, out of the other implementations of the same method.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;func1&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;func1&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the compiler sees the &lt;code&gt;func1();&lt;/code&gt; function call at compile time, then it will bind that call with the &lt;code&gt;int func1(){.....}&lt;/code&gt; definition.&lt;/p&gt;

&lt;p&gt;If the compiler sees &lt;code&gt;func1(a,b);&lt;/code&gt; at compile time, then it will bind that call with the &lt;code&gt;void func1(int a,int b){.....}&lt;/code&gt; definition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method Overriding
&lt;/h2&gt;

&lt;p&gt;If we consider the case of method overriding, at compile time only, the machine gets to know which object is getting instantiated, like base or derived.&lt;/p&gt;

&lt;p&gt;For example, we write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;BaseClass&lt;/span&gt; &lt;span class="n"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DerivedClass&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="n"&gt;object&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Func1&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know that the compiler is not that dumb. It can easily differentiate the words &lt;code&gt;DerivedClass&lt;/code&gt; and &lt;code&gt;BaseClass&lt;/code&gt;, and it has that much sense to bind the method present inside &lt;code&gt;DerivedClass&lt;/code&gt; with the function call &lt;code&gt;object.func1();&lt;/code&gt; at compile time itself, but why doesn't it bind at that time and waste time in binding that at run time?&lt;/p&gt;

&lt;p&gt;Imagine if we do so, then both become compile-time polymorphism only, and we will not have any confusion in studying run-time and compile-time polymorphism.&lt;/p&gt;

&lt;p&gt;Why doesn't Java do that? Why does Java confuse us with run-time and compile-time polymorphism?&lt;/p&gt;

&lt;p&gt;The answer to that question is explained below.&lt;/p&gt;

&lt;p&gt;In fact, modern Java compilers and the Just-In-Time (JIT) compiler do exactly what we argued above. It is called Monomorphic Inline Caching or Devirtualization. When the compiler sees the object creation right next to the function call to the same object, then it bypasses the runtime lookup and binds it directly at compile time to save performance.&lt;/p&gt;

&lt;p&gt;But when an object is created inside if-else or switch-case statements, then it becomes impossible to bind the function call with the method at compile time. To understand this, look at the gaming example given below.&lt;/p&gt;

&lt;p&gt;I will explain that with the simple example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overloading Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;weapon&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;rightHand&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;                         &lt;span class="c1"&gt;// Method A&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;weapon&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;rightHand&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;leftHand&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;       &lt;span class="c1"&gt;// Method B&lt;/span&gt;


&lt;span class="c1"&gt;// main method code&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;choice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getPlayerInput&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Input happens at runtime&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;weapon&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"swordInRightHand"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// function call 1&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="n"&gt;weapon&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"gunInRightHand"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"swordInLeftHand"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// function call 2&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// there are two function calls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Overriding Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// assume Weapon is the base class and SwordInRightHand,&lt;/span&gt;
&lt;span class="c1"&gt;// gunInRightHandAndSwordInLeftHand as derived classes&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;choice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getPlayerInput&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// User presses a button on their keyboard at runtime&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Weapon&lt;/span&gt; &lt;span class="n"&gt;weapon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SwordInRightHand&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nc"&gt;Weapon&lt;/span&gt; &lt;span class="n"&gt;weapon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;gunInRightHandAndSwordInLeftHand&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;weapon&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;attack&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// only one function call present&lt;/span&gt;

&lt;span class="c1"&gt;// The compiler looks at this line at compile time, but it was not aware of which control statement gets executed in the future.&lt;/span&gt;
&lt;span class="c1"&gt;// So it won't bind this line with either SwordInRightHand()&lt;/span&gt;
&lt;span class="c1"&gt;// or gunInRightHandAndSwordInLeftHand() ......!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We note that in the example of overloading, we have two different function calls for two different definitions, but in the example of overriding, we have only one function call which can be bound to two different definitions.&lt;/p&gt;

&lt;p&gt;So in overloading, the compiler easily identifies that there are two function calls, and it also finds the respective two different implementations.&lt;/p&gt;

&lt;p&gt;But in the example of overriding, the compiler successfully gets to know that there are two different implementations, but it has only one function call.&lt;/p&gt;

&lt;p&gt;So that's the reason it leaves the binding of the method with the function call at run time and gives privilege to the machine to bind the function call with that method by analysing which class object is created at run time.&lt;/p&gt;

&lt;p&gt;As Java is the broader language, and sometimes run-time decisions are made even without the explicit use of decision-control statements in many other cases, explaining that further will increase the size of the article.&lt;/p&gt;

&lt;p&gt;So in this, I am just using decision-control statements as a major reason for the existence of run-time polymorphism.&lt;/p&gt;

&lt;p&gt;I hope this article is helpful in understanding the difference between run-time and compile-time polymorphism.&lt;/p&gt;

&lt;p&gt;Thank you so much.......&lt;/p&gt;

</description>
      <category>oop</category>
      <category>java</category>
    </item>
  </channel>
</rss>
