Polymorphism In Java

The objective of this post is to provide learners with a better understanding of Java polymorphism by presenting 2 types of polymorphism:

  1. Compile time Polymorphism.
  2. Runtime Polymorphism.


Java Polymorphism – Compile Time

Compile-time polymorphism can be achieved by the means of method overloading. Having multiple inheritance-related classes results in polymorphism, which means “many forms”.

Inheritance lets us utilize the attributes and methods of another class just as we described in the previous chapter.

Polymorphism uses those methods to accomplish various tasks. Using this technique, we can perform the same action in different ways.

Furthermore, we can use Java method overloading and overriding to implement Java polymorphism.

Remember: Having multiple methods with the same name is referred to as method overloading.

For instance, assume a MotorBikes (superclass) with a founder_name() method.

MotorBikes can be classified as Cruisers, SportsBikes, Scooters, and Touring Bikes.

They also have their respective founders (Arthur Hugo Cecil Gibson of scooters, John Foster Fraser of touring bikes, etc.):

Example: 

class MotorBikes{void founder_name(){System.out.println("The Founder of Motorcycle is: Giuseppe Murnigott "); } }class SportsBikes extends MotorBikes{void founder_name(){System.out.println("The Founder of SportsBikes is: Gottlieb Daimler "); }}class TouringBikes extends MotorBikes{void founder_name(){System.out.println("The Founder of TouringBikes is: John Foster Fraser "); }}

Also electronic gadgets can be categorized into Robots, Mobile Phones, Laptops, and others. Additionally, they have a method called mrx_desc() that describes themselves:

Example: 

class Electronic_Gadgets{ void mrx_desc() { System.out.println("There is a variety of gadgets in Electronics");}}class MobilePhones extends Electronic_Gadgets{void mrx_desc() { System.out.println("Mobile Phone is one of the type of an Electronic gadgets");}}class Robots extends Electronic_Gadgets{void mrx_desc() { System.out.println("Robot is the most modern type of an Electronic gadget");} }

 

Remember: We used the extends keyword in the Inheritance chapter to inherit from classes.

Mobile Phones and Robots objects can now be created and mrx_desc() can be called on each:

Example: 

class Robots extends Electronic_Gadgets{void mrx_desc() { System.out.println("Robot is the most modern type of an Electronic gadget");}}public class Polymorphism1 { public static void main(String[] args) {MobilePhones my_object1=new MobilePhones(); // Object of Subclass(MobilePhones) is created here my_object1.mrx_desc(); // Method named mrx_desc() is used hereRobots my_object2=new Robots(); // Object of Subclass(Robot) is created here my_object2.mrx_desc(); // Method is invoked using my_object_2}// Output 1: Mobile Phone is one of the typeof an Electronic gadgets // Output 2: Robot is the most modern typeof an Electronic gadgets }

Java Polymorphism – Runtime

An overridden method is called at runtime rather than compiled, and this is known as runtime polymorphism or dynamic method dispatch.

Java Polymorphism works on the principle of “Upcasting”.

Upcasting is a process of referring to the object of Subclass by a reference variable from Super class is known as upcasting.

The Example given below defines the phenomenon of runtime polymorphism:

Example: 

class MotorBikes{void founder_name(){System.out.println("The Founder of Motorcycle is: Giuseppe Murnigott "); } }class SportsBikes extends MotorBikes{void founder_name(){System.out.println("The Founder of SportsBikes is: Gottlieb Daimler "); }public static void main(String[] args) {MotorBikes my_obj=new SportsBikes(); // This is an example of runtime polymorphism (variable of Superclass refers to the object of subclass) my_obj.founder_name(); // Prints the method of the extended class } // Output: The Founder of SportsBikes is: Gottlieb Daimler }

The example below illustrates the use of variables implementing the Runtime-polymorphism.

Example: 

class ElectronicGadgets{String count="7.68 Billion"; void mrx_desc() { System.out.println("There is a variety of gadgets in Electronics");}}class Mobile_Phones extends ElectronicGadgets{String count="7.12 Billion"; void mrx_desc() { System.out.println("Mobile Phone is one of the type of an Electronic gadgets");}public static void main(String[] args) { ElectronicGadgets my_obj=new Mobile_Phones(); // Runtime Polymorphism is implemented here my_obj.mrx_desc(); // Object calls method mrx_desc() of the Mobile Phones class System.out.println("Total Electronic gadgets users: "+my_obj.count); // Prints value of count from the parent (ElectronicGadgets class)}}

Using “Inheritance” and “Polymorphism”: Why and When?

It helps with code reusability: When creating a new class, reuse attributes and methods of an existing one.

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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