Polymorphism in java with Example Program

In this article we will discuss how polymorphism works in java or how we can achieve polymorphism in java?

Polymorphism is the ability of an object to take on many forms. In Java, polymorphism is the capability of an action or method to do different things based on the input and object that it is acting upon.

Polymorphism means—one name many form.

Type of polymorphism in java:

  1. Compile time
  2. Run time            
  1. Compile Time: 

Compile time polymorphism in java we can achieve by method overloading.

If in same class method name is same but the number of input parameters and types of parameters are different, then this is called method overloading in Java.

This is also called static polymorphism.

Overloaded methods properties:

  1. Exist in the same class or a subclass.
  2. They have same name.
  3. They have different input parameter
  4. May have different return types

Example:

public class TestOverLoading { 

// Without input parameter and return type

public void talk() {

System.out.println(“Example 1”);

}

 

// With one input parameter and Without return type

public void talk(int a) {

System.out.println(“Example 2 ” + a);

}

 

// With two input parameter and Without return type

public void talk(int a, int b) {

System.out.println(“Example 3 ” + a + “–” + b);

}

 

// With three input parameter and Without return type

public void talk(int a, int b, int c) {

System.out.println(“Example 4 ” + a + “–” + b + “–” + c);

}

 

// With one input parameter and different type and return type

public String talk(String a) {

System.out.println(“Example 5 ” + a);

return a;

}

 

public static void main(String[] args) {

TestOverLoading obj = new TestOverLoading();

obj.talk();

obj.talk(2);

obj.talk(2, 3);

obj.talk(2, 3, 4);

obj.talk(“test”);

}

 

}

 

Output:

Example 1Example 2 2

Example 3 2–3

Example 4 2–3–4

Example 5 test

2.     Run Time:

Run time polymorphism we can achieve by method overriding.

Hence function call resolution takes place at run time. This mechanism is achieved through inheritance and method overriding.

This is also called dynamic polymorphism.

Dynamic polymorphism means that the information required to call a method is not known until run time.

If a child class have similar method which is present in parent or base class. Child class and parent class methods have same signature then this is called method overriding in java.

Overriding methods properties:

  1. Abstract method must be override.
  2. We cannot override static method.
  3. We cannot override final method.
  4. We cannot override constructors.
  5. Only base class method can override.
  6. Override method must have the same argument list.
  7. Override method must have the same return type.

Example:

class A{

int  method1(int a, int b){

}

 

}

 

class B extends A {

int  method1(int a, int b) {

 

}

 

}

java

No Responses

Leave a Reply

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