Polymorphism in Java

by | May 25, 2021 | Java

Polymorphism in Java

The meaning of the word ‘polymorphism’ is having many forms. In lay words, polymorphism is the ability of a message to be displayed in several forms. A simple real-time example of polymorphism is the role of a human being in the life of several people. Like a man can be a father, husband, friend, and employee at the same time. Similarly, a woman can be a mother, wife, and so on. Although it is the same person who is the subject of these relationships, the declaration of the relationship makes the person unique to different people. Therefore, the same person possesses several different behaviours. This property can also be extended to Java and is called polymorphism.

Polymorphism is one of the fundamental pillars of OOP. It allows one action to be performed in several different ways. Polymorphism can also be thought of as defining one interface while having multiple implementations. When broken down, ‘poly’ means many while ‘morph’ means forms.

Types of Polymorphism

In Java, polymorphism can be broadly segregated into two broad types:

  • Compile-time polymorphism
  • Runtime polymorphism

Compile-time polymorphism

Compile-time polymorphism is often referred to as static polymorphism. You can achieve this either by function overloading or operator loading. As Java does not support operator overloading, the following example will demonstrate method overloading.

Method Overloading

Method Overloading takes place when your code has several methods with the same name, and you want to uniquely identify and call each method without having to change the name. In such cases, the argument or the parameter list of the methods are not the same. Functions can be overloaded either by changing the number of arguments or by changing the type of arguments.

Example: By using different types of arguments

class Demo1
{
static int Multiply (int a, int b)
{
return a * b;
}
static double Multiply (double a, double b)
{
return a * b;
}
}
class Main
{
public static void main (String args [])
{
System.out.println (Demo1.muliply (2,4));
System.out.println (Demo1.multiply (5.5, 6.3));
}
}

 

OUTPUT
8
34.65

Example: By using different number of arguments

class Demo2
{
static int Multiply (int a, int b)
{
return a * b;
}
static int Multiply (int a, int b, int c)
{
return a * b * c;
}
}
class Main
{
public static void main (String args [])
{
System.out.println (Demo2.muliply (2,4));
System.out.println (Demo2.multiply (2, 7, 3));
}
}

 

OUTPUT
8
42

Runtime Polymorphism

Runtime polymorphism is also known as Dynamic Method Dispatch. ‘It is a process in which a function call to an overridden method is resolved.’ While compile-time polymorphism is achieved through method overloading, runtime polymorphism is achieved through method overriding.

Method Overriding

When a subclass has a method declaration for one of the methods of the superclass, the superclass function is overridden, and the subclass method is called.

Sample Code

class Parent
{
void Print ()
{
System.out.println (“parent class”);
}
}
class subclass1 extends Parent
{
void Print ()
{
System.out.println (“subclass 1”);
}
}
class subclass2 extends Parent {
void Print ()
{
System.out.println (“subclass 2”);
}
}
class Test
{
public static void main (String args [])
{
Parent a;
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}

 

OUTPUT

subclass1
subclass2

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.