• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Polymorphism In Java With Simple Explanation & Examples

Polymorphism In Java With Simple Explanation & Examples

Last Updated on: August 15, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • What Is Polymorphism?
  • Types of polymorphism in Java
  • Compile Time Or Static Polymorphism
  • Dynamic or Runtime Polymorphism

Welcome to another brand new post on Core Java tutorial Series, in our last post, we have discussed the importance and how to use the method overriding & method hiding in Java. In this post, we are going to discuss another important feature of the Java OOPS concept, which is polymorphism.

Polymorphism allows us to perform a single action but in different ways. The word polymorphism comes from a Greek language and which means is “Many Forms.” In simple words, we can say that this is the ability of Java programming language by which “we can create functions or reference variables which behave differently in a different programming context.”

What Is Polymorphism?

Because of polymorphism, Java programming language methods can do different things based on the object that is acting upon. In other words, we define one interface and have multiple implementations.

I hope you have got an idea about these important topics. Now let’s move further and understand the different types of polymorphism in Java with examples.

Types of polymorphism in Java

Java programming language supports two types of polymorphism, and that is:

  • Static Polymorphism (Compile-time)
  • Dynamic Polymorphism (Runtime)
Polymorphism In Java With Explanation
Polymorphism In Java With Explanation

Compile Time Or Static Polymorphism

A polymorphism that is resolved during the compile time is called static polymorphism. Method overloading is an example of compile-time polymorphism.

Method Overloading: This allows us to have more than one method having the same name but different parameter lists where the sequence and data types of parameters are different. For more information, you can refer to our Method Overloading post.

Static Polymorphism Example: Method overloading is one of the ways Java supports static polymorphism.

package com.SoftwareTestingo.JavaBasics;
class PolymorphismEx
{
   public void displayString(String str)
   {
      System.out.println("Single Parameterized Display Method Executed");
   }
   public void displayString(String str, String str1) 
   {
      System.out.println("Double Parameterized Display Method Got Executed");
      
   }
}
public class Static_Polymorphism 
{
   public static void main(String[] args) 
   {
      ParentClassEx obj=new ParentClassEx();
      obj.displayString("Welcome"); // Will Call Single Parameterized Method
      obj.displayString("WELCOME", "USER");// Will Call Double Parameterized Method
   }
}

Output:

Single Parameterized Display Method Executed
Double Parameterized Display Method Got Executed

Dynamic or Runtime Polymorphism

Dynamic Polymorphism is a process which calls to the overridden methods and resolves at runtime. That’s why it is called runtime polymorphism. We have a full post where we have discussed Method Overriding in Java.

package com.SoftwareTestingo.JavaBasics;

class Vehicle
{
   public void speed()
   {
      System.out.println("Speed Method of Vehicle Class Executed");
   }
}
class Bike extends Vehicle
{
   public void speed()
   {
      System.out.println("Speed Method Of Bike Class Executed");
   }
}
public class MethodDispatch extends Vehicle
{
   public static void main(String[] args) 
   {
      Vehicle obj=new Vehicle();
      Vehicle obj1=new Bike();
      
      obj.speed();
      obj1.speed();

   }
}

Output:

Speed Method of Vehicle Class Executed
Speed Method Of Bike Class Executed

When a user wants to call an overridden method by using a parent class reference, then the type of the object determines which method is going to execute is determined at run time. Since both the classes have the same method at that time, which method is going to call is determined at runtime by JVM.

Ref – article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers