What Is Polymorphism In Object-Oriented Programming? Great 1 Of OOP

Polymorphism In Object-Oriented Programming
Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Share on pinterest
Share on print

Polymorphism in Object-Oriented Programming name describes the meaning. Poly into Many and morphism into Form. Then Polymorphism easily says a single class or object 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 relationship with a single class.

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

Real-World Example in 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 smartphone not only for that but it plays 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
Father as an Employer

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 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 described 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 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.

Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Share on pinterest
Share on print

6 thoughts on “What Is Polymorphism In Object-Oriented Programming? Great 1 Of OOP”

  1. What’s Happening i am new to this, I stumbled upon this I have found It absolutely useful and it has aided me out loads. I hope to contribute & assist other users like its aided me. Great job.

Leave a Comment

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

Related Articles
You May Like
Subscribe to our Newsletter
Scroll to Top