<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">The dictionary definition of <i>polymorphism</i> refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.</p>
<p> Polymorphism can be demonstrated with a minor modification to the <i><b>Bicycle </b></i>class. For example, a <i><b>printDescription </b></i>method could be added to the class that displays all the data currently stored in an instance. </div>
<p></p>
<pre class="highlight" name="code">public void printDescription(){
 System.out.println("nBike is " + "in gear " + this.gear
 + " with a cadence of " + this.cadence +
 " and travelling at a speed of " + this.speed + ". ");
}
</pre>
<div align='center' id="ads-id"></div>
<p>To demonstrate polymorphic features in the Java language, extend the Bicycle class with a MountainBike and a RoadBike class. For MountainBike, add a field for suspension, which is a String value that indicates if the bike has a front shock absorber, Front. Or, the bike has a front and back shock absorber, Dual.</p>
<p> <b>Here is the updated class:</b></div>
<pre class="highlight" name="code">public class MountainBike extends Bicycle {
 private String suspension;

 public MountainBike(
 int startCadence,
 int startSpeed,
 int startGear,
 String suspensionType){
 super(startCadence,
 startSpeed,
 startGear);
 this.setSuspension(suspensionType);
 }

 public String getSuspension(){
 return this.suspension;
 }

 public void setSuspension(String suspensionType) {
 this.suspension = suspensionType;
 }

 public void printDescription() {
 super.printDescription();
 System.out.println("The " + "MountainBike has a" +
 getSuspension() + " suspension.");
 }
} 
</pre>
<p>
Note the overridden printDescription method. In addition to the information provided before, additional data about the suspension is included to the output.</p>
<p> Next, create the RoadBike class. Because road or racing bikes have skinny tires, add an attribute to track the tire width. Here is the RoadBike class:</p></div>
<pre class="highlight" name="code">public class RoadBike extends Bicycle{
 // In millimeters (mm)
 private int tireWidth;

 public RoadBike(int startCadence,
 int startSpeed,
 int startGear,
 int newTireWidth){
 super(startCadence,
 startSpeed,
 startGear);
 this.setTireWidth(newTireWidth);
 }

 public int getTireWidth(){
 return this.tireWidth;
 }

 public void setTireWidth(int newTireWidth){
 this.tireWidth = newTireWidth;
 }

 public void printDescription(){
 super.printDescription();
 System.out.println("The RoadBike" + " has " + getTireWidth() +
 " MM tires.");
 }
}
</pre>
<p>
<b>Note</b> that once again, the printDescription method has been overridden. This time, information about the tire width is displayed.</p>
<p> To summarize, there are three classes: Bicycle, MountainBike, and RoadBike. The two subclasses override the printDescription method and print unique information.</p>
<p> Here is a test program that creates three Bicycle variables. Each variable is assigned to one of the three bicycle classes. Each variable is then printed.</p></div>
<pre class="highlight" name="code">public class TestBikes {
 public static void main(String[] args){
 Bicycle bike01, bike02, bike03;

 bike01 = new Bicycle(20, 10, 1);
 bike02 = new MountainBike(20, 10, 5, "Dual");
 bike03 = new RoadBike(40, 20, 8, 23);

 bike01.printDescription();
 bike02.printDescription();
 bike03.printDescription();
 }
}
</pre>
<p>
The following is the output from the test program:</p>
<div style="background-color: #ff99cc;"><b>output:</b><br />
Bike is in gear 1 with a cadence of 20 and travelling at a speed of 10. </p>
<p>Bike is in gear 5 with a cadence of 20 and travelling at a speed of 10. <br />
The MountainBike has a Dual suspension.</p>
<p>Bike is in gear 8 with a cadence of 40 and travelling at a speed of 20. <br />
The RoadBike has 23 MM tires.</div>
<p>
<b>Types of Polymorphism in Java:</b><br />
There are following two type polymorphism in java.</p>
<p><b>1. <a href="https://dineshonjava.com/2013/03/runtime-polymorphism-in-java.html">Run Time Polymorphism(Method overriding)</a></b><br />
<b>2. <a href="https://dineshonjava.com/2013/03/compile-time-polymorphism-in-java.html">Compile Time Polymorphism(Method overloading)</a></b></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/03/overriding-in-java.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/04/abstraction-in-java.html">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/overriding-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/runtime-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: '516'});
});
</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…