In this article Polymorphism in Java we give the information about Polymorphism means many forms, it is a geek language word made up of two words poly (many) and morph (forms). In simple words, polymorphism means many forms.

Polymorphism in java:

Polymorphism means many forms, it is a geek language word made up of two words poly (many) and morph (forms). In simple words, polymorphism means many forms.

Polymorphism is an ability by which you can access same type of resources in different ways. This concept is normally used in inheritance.

This is a feature by which we can use an object with the same name in multiple ways. The basic feature of polymorphism is that through it we can increase uniqueness and usefulness.

Types of Polymorphism in Java:

Polymorphism is achieved in two ways –

  1. Run Time Polymorphism
  2. Compile Time Polymorphism

Compile Time Polymorphism in Java:

This type of polymorphism is achieved by method overloading. It is also called static or early binding because parameters are bind at compile time. Overloaded methods are invoked at compile time according to the number of arguments passed. In this, the compiler decides at compile time and selects the most appropriate method.

polymorphism in java example:

File Name: Arithmatic.java

class Total

{

  static int tot(int j,int k)

{

            return j+k;

  }

    // define another method with different numbers of parameters.

  static int tot(int u,int v,int w)

{

            return u+v+w;

  }

}

public class Arithmatic

{

  public static void main(String[] args)

{

    System.out.println(Total.tot(13,5));

    System.out.println(Total.tot(6,2,14));

  }

}

Output:-

Compile Program: javac Arithmatic.java

Run Program: java Arithmatic

18

22

Run Time Polymorphism in Java:

This type of polymorphism is achieved by method overriding. In this, apart from the base class, the object method is also defined in the child class. Runtime polymorphism is also called Dynamic Method Dispatch, where the overridden method is resolved at runtime.

polymorphism in java example:

File: ABC.java

class Game

{

  void play()

{

    System.out.println(“Game…there are two types indoors and outdoors.”);

  }

}

class Cricket extends Game

{

  void play()

{

    System.out.println(“Cricket…It is outdoor game.”);

  }

}

class Chess extends Game

{

  void play()

{

    System.out.println(“Chess…It is indoor game.”);

  }

}

// main class.

public class ABC

{

  public static void main(String args[])

{

    Cricket c = new Cricket();  // create Cricket object.

    Chess d = new Chess(); // create Chess object.

    c.play();

    d.play();

  }

}

Output

Compile Program: javac ABC.java

Run Program: java ABC

Cricket…It is outdoor game.

Chess…It is indoor game.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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