Dremendo Tag Line

Polymorphism in Java Programming

OOPs in Java

In this lesson, we will understand what is Polymorphism in Java and how to implement them in programming along with some examples.

What is Polymorphism in Java?

The term Polymorphism has been derived from the words poly means many and morphism means form. In programming, polymorphism means to create many forms from one.

There are two types of polymorphism available in Java and they are:

  • Compile Time Polymorphism
  • Runtime Polymorphism

Let's discuss each of them in detail one by one.

video-poster

Compile Time Polymorphism

In Java, compile time polymorphism refers to the ability of the compiler to select the appropriate function implementation to use at compile time, based on the types of the arguments that are passed to it. This is also known as static binding or early binding.

In Java, compile time polymorphism can be achieve using:

  • Function Overloading

Function Overloading

Function Overloading is a process in which we declare a function that can perform a different task when provided with a different number of inputs.

For example, we will overload a function called area, which, when called with one argument, will display the area of a square. When the same method is called with two arguments, it will display the area of a rectangle.

Example of Function Overloading

public class Example
{
    public static void area(int side)
    {
        int a;
        a=side*side;
        System.out.println("Area of the square = " + a);
    }

    public static void area(int l,int b)
    {
        int a;
        a=l*b;
        System.out.println("Area of the rectangle = " + a);
    }

    public static void main(String args[])
    {
        area(5);
        area(10,4);
    }
}

Output

Area of the square = 25
Area of the rectangle = 40

In the above program, we declared two functions having the same name but with different numbers of arguments. When we call the area() function with one argument, it prints the square's area.

On the other hand, when we call the area() function with two arguments, it prints the rectangle's area.

Runtime Polymorphism

In Java, runtime polymorphism is a programming technique where the behavior of a member function of a class is determined at runtime rather than compile time. This is also known as Dynamic Method Dispatch.

In Java, runtime polymorphism can be achieved using:

  • Method Overriding

What is Method Overriding in Java

Method Overriding is a process used in inheritance in which a base class method is redeclared with a new body in a subclass.

Example of Method Overriding

class Shape
{
    public void draw()
    {
        System.out.println("Drawing a shape");
    }
}

class Circle extends Shape
{
    public void draw()
    {
        System.out.println("Drawing a circle");
    }
}

public class Example
{
    public static void main(String args[])
    {
        Shape x;
        x=new Circle();
        x.draw();
    }
}

Output

Drawing a circle

In the above example, the draw method of Shape class is overridden in the Circle class. When the draw method is called through the reference variable x of the base class Shape, the compiler will determine at runtime that the object pointed to variable x is actually a Circle class, so it executed the draw method in the Circle class rather than the one in the Shape class. This is an example of runtime polymorphism.