How to work Polymorphism in Object orient Programming

Asiri Gunasena

Published:

Object Oriented Programming

Polymorphism in Object orient Programming

Polymorphism in Object orient Programming name describes the meaning. Poly into Many and morphism into Form. Then Polymorphism easily says a single class or object’s ability to play as many forms. In other words, there can be more characteristics with one class according to the situation.

In this article, we hope to simply explain what is polymorphism in Java (polymorphism in Java), polymorphism in Java example, runtime polymorphism in Java, compile time polymorphism in Java

In Programming, Polymorphism is made through inheritance. If a single class inherits many classes, then the child class has all the abilities of the parent class. As we discuss in the inheritance there are many IS A relationships with a single class.

Polymorphism is everywhere. This will easily be understood by real-world Polymorphism examples, let’s move on to it.

Real-World Example of Polymorphism

Example 1

Let’s first take the smartphone as an example. The mobile phone has invented for communication in between two persons who are not in the same place. But now smartphones not only for that but it play as a camera, an internet browser, drawing and many more.

When considering communication there are a lot of behaviors. Communication is possible through direct calls, SMS, email, and messenger apps. Then the smartphone is called a Polymorphic object in the real world.

Example 2
Polymorphism in Object orient Programming
Polymorphism

Let’s take another example as humans. Your father is a human in your house. But he is the employee in his working office or location. Also, he is the husband to your mother. Maybe a Social worker in society. The role and the behaviors change according to the situation and location.

Those are examples of Polymorphism in the real world and are very easy to understand using real-world examples. Let’s see how Polymorphism implemented in Java

Polymorphism Real-Life Example with Java

Java supports Polymorphism through inheritance. Java does not support multiple class extends but it’s possible to implement multiple interfaces. Let’s take our Smart mobile as an example to implement in Java.

Polymorphism through Inheritance Class Diagram
// Camera behaviour is take Photos
interface Camera {
   void takePhoto();
}

// WebBrowser behaviour is browse through the internet
interface WebBrowser{
   public void browse();
}

// Email behaviour is send mails
interface Email{
   public void sendMail();
}

// BasicPhone behaviour is take calls
class BasicPhone{
   public void takeCall(){
      System.out.println("Taking call ");
   }
}

// Once inherites all the above classes MobilePhone 
// work as Polymorphic Object according to the situation
class MobilePhone extends BasicPhone implements Camera,WebBrowser,Email{
   public void sendMail(){
      System.out.println("sendMail ");
   }
   public void browse(){
      System.out.println("browse ");
   }
   public void takePhoto(){
      System.out.println("takePhoto ");
   }
}

public class Polymorphism {
   public static void main(String[] args) {
      MobilePhone mobilePhone = new MobilePhone();
      // MobilePhone IS-A Camera
      Camera camera=mobilePhone;
      // MobilePhone IS-A WebBrowser
      WebBrowser webBrowser=mobilePhone;
      // MobilePhone IS-A Email Sender
      Email email=mobilePhone;
      // MobilePhone IS-A BasicPhone
      BasicPhone basicPhone=mobilePhone;
   }
}

What are the types of Polymorphism in Object orient Programming

  • Compile-time Polymorphism: Static Polymorphism
  • Runtime Polymorphism: Dynamic Polymorphism

What is compile-time Polymorphism (Static Polymorphism)?

Method Overloading

In a Java class able to create methods with the same name but with different parameters. This is called method overloading. When method overloading, the compiler (Compile-time) is able to decide which method runs at runtime.

Method signature changes according to the parameters in the overloading. Therefore the compiler sees the difference between each method and decides which method to run. That’s called compile-time binding or static binding.

What is Runtime Polymorphism (Dynamic Polymorphism)?

Method Overring

The above describes how polymorphism works. Run time polymorphism is through inheritance. Inheritance hierarchy subclass methods derive from a parent class and in subclass reimplementing a parent class method with the same signature is called method overriding.

Which overridden function to run at runtime is decided by the JVM. It’s called runtime binding or dynamic binding. The overriding method should have the same signature (same name and same parameter). Reference of the object can be any parent type class of the inheritance hierarchy. But in the run time JVM runs only the Object type method.

What are the Advantages of Polymorphism?

  • Code reusability:  It’s through inheritance.
  • Easy to extend through the overloading and overriding
  • Code quality improves

Summary

Polymorphism in Object orient Programming is one of the best practices in the implementation of programming.  It helps to code reuse and write creative code blocks.  Polymorphism easily says single class or object plays as many forms.

Use the same name but with different parameters to overload the method in a single class. It’s compile-time overloading or static overloading.

Overriding is one of the most used methodologies in programming. Write to method with the same signature but changing body inside the method.  JVM decides at runtime which method to run this time. Therefore this is called runtime Polymorphism or dynamic Polymorphism.

ennicode

Address: 89/1 Rabbegamuwa, Handessa, Kandy, Central, Sri Lanka

Email to: Primary: [email protected]

Services

E-Learning

Company Websites

Support and Configuration work

Banners, Covers, and Post

Web Development & Configurations

Content Writing and Marketing

Contact

Ennicode