<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.</div>
<p><b>Upcasting:</b></p>
<p>When reference variable of Parent class refers to the object of Child class, it is known as <b>upcasting</b>.<br />
<b>For example:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/03/upcasting.jpg" border="0" /></div>
</div>
<pre class="highlight">class A{} 
class B extends A{} 
</pre>
<pre class="highlight">class Test{ 
 public static void main(String args[]){ 
 A a=new B();//upcasting 
 } 
} 
</pre>
<div id="ads-id" align="center"></div>
<p><b>Example of Runtime Polymorphism:</b></p>
<p>In this example, we are creating two classes <i><b>Bicycle </b></i>and <i><b>HondaShine</b></i>. <b><i>HondaShine </i></b>class extends <i><b>Bicycle</b></i> class and overrides its<b> run() </b>method. We are calling the run method by the reference variable of Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, subclass method is invoked at runtime. Since it is determined by the compiler, which method will be invoked at runtime, so it is known as runtime polymorphism.</p>
</div>
<p> ;</p>
<pre class="highlight">class Bicycle{ 
 void run(){ 
 System.out.println("bicycle is running"); 
 } 
 } 
 class HondaShine extends Bicycle{ 
 void run(){ 
 System.out.println("shine is running safely with 70km"); 
 } 
 
 public static void main(String args[]){ 
 Bicycle b = new HondaShine();//upcasting 
 b.run(); 
 } 
 } 
</pre>
<p> ;</p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
shine is running safely with 70km</div>
<p><b>Runtime Polymorphism with data member:</b></p>
<p>Method is overriden not the datamembers, so runtime polymorphism can&#8217;t be achieved by data members.<br />
In the example given below, both the classes have a datamember speedlimit, we are accessing the datamember by the reference variable of Parent class which refers to the subclass object. Since we are accessing the datamember which is not overridden, hence it will access the datamember of Parent class always.</p>
<pre class="highlight">class Bicycle{ 
 int speedlimit=100; 
 } 
 class HondaShine extends Bicycle{ 
 int speedlimit=160; 
 
 public static void main(String args[]){ 
 Bicycle obj=new HondaShine(); 
 System.out.println(obj.speedlimit); 
 } 
} 
</pre>
<p> ;</p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
<b>10</b>0</div>
<p><b>Note:</b> <b>Runtime polymorphism can&#8217;t be achieved by data members.</b></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/polymorphism-in-java/">Previous</a> <;<; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>|| >;>;<a href="https://dineshonjava.com/polymorphism-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/polymorphism-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/compile-time-polymorphism-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '515'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…