• August 23, 2014
  • Core Java
  • Comments Off on Polymorphism in java

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.

PolymorphismExample.java

/**
 * This program is used to show simple example of polymorphism.
 * @author javawithease
 */
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.");
      }
}

Output:

50
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.

Related Topics:

OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.

 


Comments are closed.