Polymorphism in java

Polymorphism in real world:

Polymorphism means more than one forms. Water can be of in any form solid, liquid or gas.

Polymorphism in programming:

In java polymorphism is a way in which something behaves differently based on its call. Let us take the example of + operator and see the below example.


 * This program is used to show simple example of polymorphism.
 * @author W3spoint
public class PolymorphismExample {
      public static void main(String args[]){
            //+ operator add two numeric values
            System.out.println(20 + 30);
            //+ operator concatenate two strings.
            System.out.println("hello " + "java.");


hello java.

Download this example.

In the above example + operator behaves differently based on type of argument. When arguments are of integer type it acts as additional operator and when arguments are of String type it acts as concatenation operator.

Polymorphic object:

In java an object which can pass two or more IS-A test. All objects in java are polymorphic in nature because they passed IS-A test at least for its own type and Object class type.

Types of polymorphism:

  1. Static/compile time polymorphism (by method overloading).
  2. Dynamic/run time polymorphism (by method overriding).

  Next Topic: Method overloading. Previous Topic: Encapsulation in java.


Please follow and like us:
Content Protection by DMCA.com